所以我有這個有趣的問題... 我想要訪問從列表中檢索的對象的方法,並通過ArrayList進行擴展。ArrayList擴展中的Cast對象沒有看到它的方法
它看起來像這樣:
import java.util.ArrayList;
import java.util.Collection;
public class PropertyList<Property> extends ArrayList {
private static final long serialVersionUID = -7854888805136619636L;
PropertyList(){
super();
}
PropertyList(Collection<Property> c){
super(c);
}
boolean containsProperty(PropertyList<Property> pl){
Property asdf = (Property) this.get(4);
System.out.println(asdf.<can't access "Property" methods>); //mark
return false;
}
}
任何想法,爲什麼我不能訪問標記線的方法呢? 澄清 - Property對象中的方法是公共的。
編輯: 屬性說明:
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlValue;
@XmlAccessorType(XmlAccessType.FIELD)
class Property {
@XmlValue
private String property = null;
@XmlAttribute
String state;
@XmlAttribute
String name;
@XmlAttribute
String type;
@XmlAttribute
String value;
Property(){}
Property(String state, String name, String type, String value){
this.state = state;
this.name = name;
this.type = type;
this.value = value;
}
public String getName(){
return name;
}
@Override
public String toString(){
return state + " " + name + " " + type + " " + value;
}
}
不應該'擴展ArrayList'?不管怎麼樣,你不能顯示'Property'的代碼嗎? –
Shark
@Shark的確。就目前而言,這是毫無意義的。但是,顯式強制轉換爲'Property'使我想看到定義:) – jensgram
在任何情況下,您的類聲明都不應該如下所示:'public class PropertyList extends ArrayList '您可以輕鬆地對' T'到'Property'? –
Shark