简体   繁体   中英

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. Ie. if type=Grape display seedCount.

  1. If you want to sort Fruit, you need to implement the Comparable interface for the Fruit class.

  2. If you want to display attributes, have an abstract method in Fruit class and provide the implementations in the subclass. This way depending on the instance of the Fruit , it will show the corresponding properties.

    public abstract class Fruit implements 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()

You would then be forced to impliment the method for all types of fruit, then in your for loop you would just call 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.

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. You loop over this list and want to wag all of the tails of the HousePets that can wag their tails. 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). 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. Better yet we could have a HousePet method called "showHappiness", and the Dog would wag its tail while the cat purred.

I think you need an instanceof operator. It allows you to check object type.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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