1

我有两个类,classB 和 classC,它们都扩展了 classLetters。在 classB 和 classC 调用的单独类中,有两个方法:getBType() 和 getCType()。两者中的代码几乎是重复的,因为它们都做同样的事情,除了一个抓取 B 类型和一个抓取 C 类型。有没有办法将这两者组合成一个名为 getType(String className) 的方法,例如,它可以检测哪个类正在调用它并根据谁调用该方法来获取正确的类型?这样我就可以使用一个方法从 classB 或 classC 调用 getType(String className) 而不必从 classB 调用 getBType() 和从 classC 调用 getCType()。这很重要,因为我将添加更多由此扩展的类,并且我想减少重复代码的数量。

Collection<classB> bstuff = new Collection<classB>();
Collection<classC> cstuff = new Collection<classC>();

public Collection<classB> getBType() {
        ArrayList<classB> b = new ArrayList<classb>();  //this can just return bstuff instead
        b.addAll(bstuff);                               //but i would like to leave this here for the example
        return b;
}

public Collection<classC> getCType() {
        ArrayList<classC> c = new ArrayList<classc>();  //this can just return cstuff instead
        c.addAll(cstuff);                               //but i would like to leave this here for the example
        return c;
}

我想过这样的事情:创建一个数据结构来保存由它们的类索引的集合:

static HashMap<String, Collection<? extends classLetters>> allLetters = new HashMa..... etc(); //just an example
//then here i would statically add each class type to the hashmap

public Collection<? extends classLetters> getType(className) {
    if (className == "classB") {
        ArrayList<classB> b = new ArrayList<classb>(); 
        b.addAll(bstuff);                              
        return b;
    }
    else if (className == "classC") {
        ArrayList<classC> c = new ArrayList<classc>(); 
        c.addAll(cstuff);                              
        return c;
    }
}

尽管如此,这仍然有重复的代码。使用泛型有可能吗?

public <T> Collection<T> getType() {
    ArrayList<T> anyLetter = new ArrayList<T>;
    anyLetter.addAll(/* somehow distingush which letter class to put here */);
    return anyLetter;
}
4

2 回答 2

3

可以使用数组、集合中的“模板”来代替继承。

List<String> ls = new ArrayList<>();
List<Integer> li = new ArrayList<>();

public static <T> List<T> copy(Collection<T> list) {
    List<T> result = new ArrayList<>();
    result.addAll(list);
    return result;
}

public List<String> getLS() {
    return copy(ls);
}

public List<Integer> getLI() {
    return copy(li);
}
于 2013-06-12T15:01:03.967 回答
1
class A{}
class B extends A{}
class C extends A{}

class Container{
     private Map<Class<? extends A>, Collection<? extends A>> things = new HashMap<Class<? extends A>, Collection<? extends A>>();

     <Athing extends A> Collection<Athing> get(Class<Athing> clazz)
     {
       return (Collection<Athing>)things.get(clazz);
     } 

     <Athing extends A> void put(Class<Athing> clazz, Athing thing)
     {
       Collection<Athing> coll = get(clazz);
       if(coll == null)
       {
         coll = new ArrayList<Athing>();
         things.put(clazz, coll);
       }

       coll.add(thing);
     }

     public static void main(String []args){
       Container con = new Container();
       con.put(B.class, new B());
       con.put(A.class, new A());
     }
}
于 2013-06-12T15:36:52.303 回答