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);
}
...
}
上記のコードでは、メソッドaddContentsは、Boxcarクラスで実行すると正常に機能するようです。ただし、Personクラスからまったく同じ方法で呼び出された場合は、動作が異なります。
これの原因は何ですか?どうすれば修正できますか?