简体   繁体   English

比较扩展对象

[英]comparing extended objects

private List<Fruit> myFruit = new Vector<Fruit>();

ok so if I have a list of different types of fruit objects how could I go through the list and compare the different objects. 好的,如果我有一个不同类型的水果对象的列表,该如何浏览该列表并比较不同的对象。

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);
  }
 }

So how would I go about sorting through the list of Fruit and displaying specific properties of these objects based on what type of object they are. 因此,我将如何对Fruit列表进行排序,并根据对象的类型显示这些对象的特定属性。 Ie. 就是 if type=Grape display seedCount. 如果type = Grape,则显示seedCount。

  1. If you want to sort Fruit, you need to implement the Comparable interface for the Fruit class. 如果要对Fruit进行排序,则需要为Fruit类实现Comparable接口。

  2. If you want to display attributes, have an abstract method in Fruit class and provide the implementations in the subclass. 如果要显示属性,请在Fruit类中使用抽象方法,并在子类中提供实现。 This way depending on the instance of the Fruit , it will show the corresponding properties. 这种方式取决于Fruit的实例,它将显示相应的属性。

    public abstract class Fruit implements Comparable{ 公共抽象类Fruit实施Comparable {

      public abstract void displayProperties(); @Override public int compareTo(Fruit otherFruit){ return 0; } } public class Banana extends Fruit { private int seedCount; @Override public void displayProperties() { // TODO Auto-generated method stub System.out.println(seedCount); } } public class Grape extends Fruit{ private String color; @Override public void displayProperties() { // TODO Auto-generated method stub System.out.println(color); } } 

You could add an abstract method to Fruit displayProperties() 您可以向Fruit displayProperties()添加抽象方法

You would then be forced to impliment the method for all types of fruit, then in your for loop you would just call displayProperties() 然后,您将不得不对所有类型的水果隐含该方法,然后在for循环中,只需调用displayProperties()

This isn't recommended, but you could use 不建议这样做,但是您可以使用

if(object instanceof Class) {
    Class temp = (Class) object
    //operate on object
}

But what you should do is create a Fruit interface that gives access to the important info related to all fruit, in general you shouldn't cast down to get extra information. 但是您应该做的是创建一个Fruit界面,该界面可以访问与所有水果相关的重要信息,通常,您不应该为了获得更多信息而放弃。

While downcasting isn't evil, it could mean you aren't taking full advantage of polymorphism. 虽然垂头丧气不是邪恶的,但这可能意味着您没有充分利用多态性。 To take advantage of polymorphism one should ensure that the supertype the subtypes share has methods to get as much info or behavior as necessary. 要利用多态性,应确保子类型共享的超类型具有获取所需信息或行为的方法。

For example, lets say you have a list of HousePets, and the HousePets consist of the subtypes Dog and Cat. 例如,假设您有一个HousePets列表,并且HousePets由子类型Dog和Cat组成。 You loop over this list and want to wag all of the tails of the HousePets that can wag their tails. 您遍历此列表,并想要摇晃HousePets可以拖尾的所有尾巴。 For simplicity there are two ways to do this, either only the Dog has the method 'wagTail()' (since Cats don't wag tails), or HousePets has a method called 'wagTail()' that is inherited by Cats and Dogs (but the Cat's version does nothing). 为简单起见,有两种方法可以执行此操作,或者仅Dog具有方法“ wagTail()”(因为Cats不会摇尾巴),或HousePets具有Cat's and Dogs继承的名为“ wagTail()”的方法(但Cat的版本不执行任何操作)。 If we choose the first example the code would look something like this: 如果选择第一个示例,则代码将如下所示:

 for(HousePet pet : petList)
     if(pet instanceof Dog) {
         ((Dog)pet).wagTail();
     }

And for the second example it would look like this: 对于第二个示例,它看起来像这样:

 for(HousePet pet : petList)
     pet.wagTail();

The second example takes a little less code and is a little more streamlined. 第二个示例少了一些代码,但简化了一些。 In the worst case scenario, if we go with the first option, we will need an if-block for each new subtype of HousePet, which will make the code bulkier/uglier/etc. 在最坏的情况下,如果我们选择第一个选项,则将为HousePet的每个新子类型需要一个if块,这将使代码更笨重/更丑陋/等。 Better yet we could have a HousePet method called "showHappiness", and the Dog would wag its tail while the cat purred. 更好的是,我们可以有一个称为“ showHappiness”的HousePet方法,当猫发出呼pur声时,狗会摇尾巴。

I think you need an instanceof operator. 我认为您需要一个instanceof运算符。 It allows you to check object type. 它允许您检查对象类型。

if (list.get(0) instanceof Grape) {
    // list.get(0) is Grape
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM