sales()无法访问以下代码中调用方法名称。arraylist 中的对象是类 Group1 which uses generic arguments. And,Division_a.list.get(0) shoud have the Group1 object. And,Division_a.list.get(0).getComponent() should returnComponent1 object. Then thesales() method should be usable.
But, the exception message shows "The methodsales()` is undefined for the type capture#2-of ?”我的谜团是Division_a.list.get(0).getComponent() 不返回 Component1 类的对象,尽管返回类型被定义为“public T getComponent()..”
import java.util.ArrayList;
public class Division_a {
public ArrayList<Group1<?>> list=null;
public Division_a() {
list=new ArrayList();
}
public void put(Group1<?> group1) {
list.add(group1);
}
public static void main(String[] args) {
Group1<Component1> groupcomponent1 = new Group1<>(new Component1());
Division_a division_a = new Division_a();
division_a.put(groupcomponent1);
division_a.list.get(0).getComponent().sales(); //excetion occur
}
}
class Component1 {
public void sales() {
System.out.println("component1 sold");
}
}
class Group1<T> {
public T component;
Group1(T component){
this.component=component;
}
public T getComponent() { //return type T
return component;
}
public void setComponent(T component) {
this.component=component;
}
}
米琪卡哇伊
四季花海
相关分类