我是新來的Java,我想知道如何做到這一點在Java或更簡單,更清潔更好,更方便,就像有多個泛型類型的東西Java的多泛型類
public class Item<T1, T2, T3, T4, T5, T6, T7, T8, T9>{
private T1 t1;
private T2 t2;
private T3 t3;
private T4 t4;
private T5 t5;
private T6 t6;
private T7 t7;
private T8 t8;
private T9 t9;
public Item(T1 t1){
this(t1, null, null, null, null, null, null, null, null);
}
public Item(T1 t1, T2 t2){
this(t1, t2, null, null, null, null, null, null, null);
}
public Item(T1 t1, T2 t2, T3 t3){
this(t1, t2, t3, null, null, null, null, null, null);
}
public Item(T1 t1, T2 t2, T3 t3, T4 t4){
this(t1, t2, t3, t4, null, null, null, null, null);
}
...
如果你真的需要9種不同gerenic類型的容器,那麼這實際上可能是最簡單的設計:d – f1sh
'公有get(INT索引){'這不是很好形成。我想你想要'public Object get(int index){' – ControlAltDel
你不需要明確地將其他字段初始化爲'null'。只需做'public Item(T1 t1){this.t1 = t1; }'等 –