private List<Fruit> myFruit = new Vector<Fruit>();
では、さまざまな種類の果物オブジェクトのリストがある場合、リストを調べてさまざまなオブジェクトを比較するにはどうすればよいでしょうか。
public class Fruit{
String type;
String color;
}
public class Grape extends Fruit{
int seedCount;
public Grape(Attributes attributes){
this.type = attributes.getValue("type");
this.color=attributes.getValue("color");
this.seedCount=attributes.getValue("seedCount");
}
public class Banana extends Fruit{
String color;
public Banana(Attributes attributes){
this.type = attributes.getValue("type");
this.color=attributes.getValue("color");
}
public load(localName name, Attributes attributes){
if (name.equalsIgnoreCase("grape"){
Grape grape = new Grape(attributes);
myFruit.add(grape);
}
if (name.equalsIgnoreCase("banana"){
Banana banana = new Banana(attributes);
myFruit.add(banana);
}
}
では、フルーツのリストを並べ替えて、オブジェクトの種類に基づいてこれらのオブジェクトの特定のプロパティを表示するにはどうすればよいでしょうか。すなわち。type=Grape の場合、seedCount を表示します。