Java 仿制药双重标准 公共级棚车{ public ArrayList contents=new ArrayList();//事物的ArrayList 公共静态void main(字符串[]args){ Boxcar测试=新Boxcar(); test.addContents(新人(“239-235-2535”,“罗纳德”,36));//作品100% } 公共Boxcar(物品类型,int maxElements){ this.type=type; boxcarId=boxcarIdCount; boxcarIdCount++; this.maxElements=maxElements; } 公共内容{ 内容。添加(事物); } ... }//终点车厢 公共阶级的人做事{ 智力年龄; 字符串govtId,name; 公众人物(字符串govtId、字符串名称、整数){ this.govtId=govtId; this.name=名称; 这个。年龄=年龄; }//端部构造器 public void load(ArrayList
Java编译器不允许访问未绑定参数化类型的引用上的方法,在您的例子中,Java 仿制药双重标准 公共级棚车{ public ArrayList contents=new ArrayList();//事物的ArrayList 公共静态void main(字符串[]args){ Boxcar测试=新Boxcar(); test.addContents(新人(“239-235-2535”,“罗纳德”,36));//作品100% } 公共Boxcar(物品类型,int maxElements){ this.type=type; boxcarId=boxcarIdCount; boxcarIdCount++; this.maxElements=maxElements; } 公共内容{ 内容。添加(事物); } ... }//终点车厢 公共阶级的人做事{ 智力年龄; 字符串govtId,name; 公众人物(字符串govtId、字符串名称、整数){ this.govtId=govtId; this.name=名称; 这个。年龄=年龄; }//端部构造器 public void load(ArrayList,java,generics,Java,Generics,Java编译器不允许访问未绑定参数化类型的引用上的方法,在您的例子中,Boxcar,因为类型未知 您应该定义通配符的边界,并按如下方式使用它: public class Boxcar<S extends Things> { public ArrayList<S> contents = new ArrayList<S>(); //an arraylist of things public static void main(String [] args){
Boxcar
,因为类型未知
您应该定义通配符的边界,并按如下方式使用它:
public class Boxcar<S extends Things> {
public ArrayList<S> contents = new ArrayList<S>(); //an arraylist of things
public static void main(String [] args){
Boxcar test = new Boxcar();
test.addContents(new Person("239-235-2535", "Ronald", 36)); //works 100%
}
public Boxcar(Things type, int maxElements){
this.type = type;
boxcarId = boxcarIdCount;
boxcarIdCount++;
this.maxElements = maxElements;
}
public void addContents(S thing) {
contents.add(thing);
}
...
}//end boxcar class
public class Person implements Things {
int age;
String govtId, name;
public Person(String govtId, String name, int age){
this.govtId = govtId;
this.name = name;
this.age = age;
}//end Consrtructor
public void load(ArrayList<Boxcar<?>> train){
Person dude = new Person("239-235-235", "Ronald", 36);
train.get(i).addContents(dude); // won't compile
}
...
}//end Person class
public interface Things {
public void load(ArrayList<Boxcar<?>> train, String [] params);
}//end interface Things
public class Train {
ArrayList<Boxcar<?>> train = new ArrayList<Boxcar<?>>();
public void load(Things thing, String [] params){
thing.load(train, params);
}
...
}
public void load(ArrayList<Boxcar<? super Things>> train)
{
Person dude = new Person("239-235-235", "Ronald", 36);
train.get(0).addContents(dude);
}
person.load(new ArrayList<Boxcar<Integer>>());
公共空荷载(ArrayList它无法编译的原因是,train
参数是一个未知类型的Boxcar
列表。实际上,这意味着其中包含未定义类型的内容
列表,而您正试图将人员
放在其中,这不是类型安全的操作。想象一下,如果您按如下方式执行加载方法:
public class Boxcar<S extends Things> {
public ArrayList<S> contents = new ArrayList<S>(); //an arraylist of things
public static void main(String [] args){
Boxcar test = new Boxcar();
test.addContents(new Person("239-235-2535", "Ronald", 36)); //works 100%
}
public Boxcar(Things type, int maxElements){
this.type = type;
boxcarId = boxcarIdCount;
boxcarIdCount++;
this.maxElements = maxElements;
}
public void addContents(S thing) {
contents.add(thing);
}
...
}//end boxcar class
public class Person implements Things {
int age;
String govtId, name;
public Person(String govtId, String name, int age){
this.govtId = govtId;
this.name = name;
this.age = age;
}//end Consrtructor
public void load(ArrayList<Boxcar<?>> train){
Person dude = new Person("239-235-235", "Ronald", 36);
train.get(i).addContents(dude); // won't compile
}
...
}//end Person class
public interface Things {
public void load(ArrayList<Boxcar<?>> train, String [] params);
}//end interface Things
public class Train {
ArrayList<Boxcar<?>> train = new ArrayList<Boxcar<?>>();
public void load(Things thing, String [] params){
thing.load(train, params);
}
...
}
public void load(ArrayList<Boxcar<? super Things>> train)
{
Person dude = new Person("239-235-235", "Ronald", 36);
train.get(0).addContents(dude);
}
person.load(new ArrayList<Boxcar<Integer>>());
你应使用:
Boxcar test = new Boxcar();
Boxcar测试=新的Boxcar();
否则,即使代码编译(带有警告),它也可能在运行时失败,并出现类强制转换异常。看起来Person
类不会编译,因为它可能没有实现东西。load
,它需要两个参数:ArrayList“完全相同”这不完全一样。首先,你有Boxcar
(原始类型)。第二,你有Boxcar
,或者更一般地说:列表谢谢这正是我需要的帮助!