我对Java相当陌生,我有一个ObjectType类的linkedList,我将ObjectType的各种子类添加到linkedList中,当我将对象拉出时,我被迫将其传递给ObjectType类型并且似乎失去了继承子类的属性....任何人都可以为我解释一下吗?
//LIST STORING OBJECT
public class MyObjectList{
private LinkedList<ObjectType> queue = new LinkedList<ObjectType>();
public MyObjectList()
{
queue = new LinkedList<ObjectType>();
}
public ObjectType addObject(ObjectType myObject)
{
queue.add(myObject);
return myObject;
}
}
//MY BASIC OBJECT
public abstract class ObjectType {
public int objectWidth;
public ObjectType () {}
}
//MY EXTENDED OBJECT BOX
public abstract class Box extends ObjectType {
public int objectWidth = 50;
public ObjectType () {}
}
//MY EXTENDED OBJECT PLATE
public abstract class Box extends ObjectType {
public int objectWidth = 25;
public Box() {}
}
// 好的,现在如果我向我的框列表中添加大量框,它们的 objectWidth 将设置为零!
mList = new MyObjectList;
for (int i=0;i<10;i++)
{
mObject = mList.addObject( new Box );
}
重要的是要记住,我的列表最终应该由不同的对象类型组成,因此 baseObject 需要保持原样。如果可能的话!