如何在arraylist中显示对象的istance? (多态性)

时间:2015-06-30 00:30:37

标签: java arraylist polymorphism

我正在尝试显示ArrayList plantList中的所有元素。 我的主程序将添加,删除,搜索,过滤和显示四个不同子类的所有植物。一切似乎"除了我显示时工作。 〜我只会包含与代码相关的部分代码。

一点背景:我是学生,这是我第一次使用继承/多态。

1)如何区分不同的物体,因为它们在显示时都有不同的参数?

2)有关如何改善我所做工作的表现/逻辑的任何建议?一点点解释都会很棒。

//父类

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


}

//几个子类中的一个

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

2 个答案:

答案 0 :(得分:4)

如果通过&#34;显示&#34;你的意思是&#34;将某种字符串打印到控制台或其他输出&#34;,然后答案很简单:根本不需要使用instanceof。您需要做的就是覆盖您想要显示的每个不同类中的toString方法,然后当您想要显示一个对象时(即使您不确切知道它是什么类型),只需在其上调用toString并打印结果即可。多态性将完成选择要调用 toString方法实现的的工作。

以下是您的具体示例。

在植物类中:

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

然后,在Flower类中:

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

最后,展示所有植物:

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

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

答案 1 :(得分:0)

你真的很亲密。在进行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");
    }
}