1

我正在尝试创建一个可以返回不同类型数据列表的方法。type参数是标识需要处理的数据类型,返回getListedData(Class type)。我还编写了一个私有方法 createList(Class type, List list, String flag),所有类型数据都可以使用它来创建列表。

代码符合我的预期,但作为泛型的新手,我不相信这是最好的编写方式。谁能给我一些建议?特别是在使用带有 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... 
            }
        }
    }

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

4

1 回答 1

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... 
    }
}
于 2013-04-25T16:18:05.450 回答