如何在数组列表中显示 object 的距离? (多态性)

How to display an istanceof an object in an arraylist? (Polymorphism)

我正在尝试显示 ArrayList plantList 中的所有元素。 我的主程序将添加、删除、搜索、过滤和显示四种不同 child class 的所有植物。一切 "seems" 都可以正常工作,除了我显示的时候。 ~我只会包含与问题相关的代码部分。

一点背景知识:我是一名学生,这是我第一次使用 inheritance/polymorphism。

1)不同的objects在显示时参数不同,如何区分?

2) 关于如何改进我正在做的事情的 performance/logic 有什么建议吗?稍微解释一下就好了。

//Parent class

public class Plant{

   private String name;
   private String id;
   private String color;

   public Plant(String name, String id, String color){
   this.name = name;
   this.id = id;
   this.color = color;

   }

   public String getName(){

      return this.name;
   }

   public void setName(String name){

      name = this.name;  
   }

   public String getId(){

      return this.id;
   }

   public void setId(String id){

      id = this.id;  
   }

   public String getColor(){

      return this.color;
   }

   public void setColor(String color){
      color = this.color;  
   }


}

//几个child中的一个classes

   public class Flower extends Plant{

   private boolean thorns;
   private boolean smell;   

   public Flower(String name, String id, String color, boolean blnThorns, boolean blnSmell){
      super(name, id, color);
      thorns = blnThorns;
      smell = blnSmell;

   }

   public boolean isThorns(){

      return thorns;
   }

   public void setThorns(boolean blnThorns){
      thorns = blnThorns;  
   }

   public boolean isSmell(){

      return smell;
   }

   public void setSmell(boolean blnSmell){
      smell = blnSmell;  
   }


}

// 部分主要驱动

ArrayList<Plant> plantList = new ArrayList<Plant>();

//adding a flower to the plantList
System.out.println("\nEnter the name of the flower to add: ");
            name = add.nextLine();

            System.out.println("\nEnter the ID code: ");
            id = add.nextLine(); 

            System.out.println("\nEnter the color: ");
            color = add.nextLine();

            System.out.println("\nAre there thorns present? (True/False) ");
            blnThorns = add.nextBoolean();

            System.out.println("\nDoes the flower smell? (True/False) ");
            blnSmell = add.nextBoolean();

            plantList.add(new Flower(name, id, color, blnThorns, blnSmell));

            System.out.println("Flower inserted.");
            System.out.println();
            break;

//displaying all plants
for( int i = 0; i < plantList.size(); i++){
      System. out.println("\t" + (i+1) + ":");
            System.out.print("\n\tName: " + plantList.get(i).getName());
            System.out.print("\n\tName: " + plantList.get(i).getId());
            System.out.print("\n\tColor: " + plantList.get(i).getColor());
            if(plantList instanceof Flower){ // HERE I am not sure what I'm doing or how to do it
            System.out.print("\n\tThorns presence: " + plantList.get(i).isThorns()); /* this is an example of what is not working properly */
            System.out.print("\n\tSmell presence: " + plantList.get(i).isSmell());  /* this is an example of what is not working properly*/
            System.out.println("\n");
            }
         }

如果 "display" 是指 "print some sort of string to the console or other output",那么答案很简单:根本不需要使用 instanceof。您需要做的就是在您想要显示的每个不同 class 中重写 toString 方法,然后在您想要显示一个对象时(即使您不确切知道它是什么类型), 只需对其调用 toString 并打印结果。多态性将完成选择 哪个 toString 方法实现来调用的工作。

这是您的具体示例中的样子。

工厂内class:

@Override
public String toString() {
   return "\n\tName: " + getName()
        + "\n\tName: " + getId()
        + "\n\tColor: " + getColor();
}

然后,花中class:

@Override
public String toString() {
   return super.toString()
        + "\n\tThorns presence: " + isThorns()
        + "\n\tSmell presence: " + isSmell();
}

最后,显示所有植物:

for (Plant plant : plantList) {
   System.out.println(plant);
}

请注意,当您将任何 Object 传递给 System.out.println 时,toString 会自动调用。

你们真的很亲密。当您进行 instanceof 检查时,您只需要检查列表的 元素 ,而不是列表本身。然后,如果它实际上是 Flower 的一个实例,那么您需要将列表元素转换为 Flower 并从那里进行方法调用。

像这样:

for(int i = 0; i < plantList.size(); i++){
    System.out.println("\t" + (i+1) + ":");
    System.out.print("\n\tName: " + plantList.get(i).getName());
    System.out.print("\n\tName: " + plantList.get(i).getId());
    System.out.print("\n\tColor: " + plantList.get(i).getColor());
    if (plantList.get(i) instanceof Flower) {
        Flower flower = (Flower)plantList.get(i);
        System.out.print("\n\tThorns presence: " + flower.isThorns());
        System.out.print("\n\tSmell presence: " + flower.isSmell());
        System.out.println("\n");
    }
}