返回带参数化类型的不同类型对象列表作为参数的方法

时间:2013-04-25 16:08:19

标签: java generics

我正在尝试创建一个可以返回不同类型数据列表的方法。 type参数用于标识需要处理和返回的数据类型getListedData(类类型)。我还写了一个私有方法createList(类类型,列表列表,字符串标志),可以被所有类型数据用来创建列表。

代码正在做我期待的事情,但作为Generics的新手,我不相信这是最好的写作方式。有人能给我一些建议吗?特别是在使用带有LIst的泛型和反射构造函数来创建对象实例时。 (我使用它的原因是使该方法可以重用于所有类型)。我对演员感到恼火:(

这些不同类型的类具有相同的结构。

    public class TypeA {
        private String propOne;
        public TypeA(String propOne) {
            super();
            this.propOne = propOne;
        }
        public String getPropOne() {
            return propOne;
        }
        public void setPropOne(String propOne) {
            this.propOne = propOne;
        }           
    }

    public class TypeB {
        private String propOne;
        public TypeB(String propOne) {
            super();
            this.propOne = propOne;
        }
        public String getPropOne() {
            return propOne;
        }
        public void setPropOne(String propOne) {
            this.propOne = propOne;
        }
    }

会有大量相同的结构化数据类型。

    public class Test {
        @SuppressWarnings("unchecked")
        public static void main(String arg[]) throws SecurityException, IllegalArgumentException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException{
            Test test = new Test();
            List<TypeB> b = (List<TypeB>) test.getListedData(TypeB.class);
            List<TypeA> a = (List<TypeA>) test.getListedData(TypeA.class);
                            //similar repeate.....
        }

        public <T> List<T> getListedData(Class<T> type) throws SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException{
            List<T> list = new ArrayList<T>();
            String flag = "";
            if(type.equals(TypeA.class)){
                flag = "A";
                createList(type, list, flag);
            }else{
                flag = "B";
                createList(type, list, flag);
            }
            return list;
        }

        private <T> void createList(Class<T> type, List<T> list, String flag)
                throws NoSuchMethodException, InstantiationException,
                IllegalAccessException, InvocationTargetException {
            for(int i=0; i<2; i++){
                Constructor<?> ctor = type.getConstructor(String.class);
                Object object = ctor.newInstance(new Object[] {String.valueOf(i)});
                list.add((T) object);
                //do something with flag... 
            }
        }
    }

任何建议将不胜感激。感谢

1 个答案:

答案 0 :(得分:0)

更好地避免案件。 您可以执行以下操作,也可以通过反射使用类的静态成员。

public <T> List<T> getListedData(Class<T> type) throws SecurityException, 
        NoSuchMethodException, IllegalArgumentException, InstantiationException,
        IllegalAccessException, InvocationTargetException {
    List<T> list = new ArrayList<>();
    String flag = type.getSimpleName();
    createList(type, list, flag);
    return list;
}

private <T> void createList(Class<T> type, List<T> list, String flag)
        throws NoSuchMethodException, InstantiationException,
        IllegalAccessException, InvocationTargetException {
    for (int i = 0; i < 2; i++) {
        Constructor<?> ctor = type.getConstructor(String.class);
        Object object = ctor.newInstance(new Object[]{String.valueOf(i)});
        list.add((T) object);
        //do something with flag... 
    }
}
相关问题