Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 比较扩展对象_Java_Oop - Fatal编程技术网

Java 比较扩展对象

Java 比较扩展对象,java,oop,Java,Oop,那么,我将如何对水果列表进行排序,并根据它们是什么类型的对象显示这些对象的特定属性呢。例如,如果type=Grape display seedCount。您可以将抽象方法添加到水果displayProperties() 然后,您将被迫为所有类型的水果实现该方法,然后在for循环中,您只需调用displayProperties()您可以向水果添加一个抽象方法displayProperties() 然后,您将被迫为所有类型的水果实现该方法,然后在for循环中只需调用displayProperties

那么,我将如何对水果列表进行排序,并根据它们是什么类型的对象显示这些对象的特定属性呢。例如,如果type=Grape display seedCount。

您可以将抽象方法添加到水果
displayProperties()


然后,您将被迫为所有类型的水果实现该方法,然后在for循环中,您只需调用
displayProperties()

您可以向水果添加一个抽象方法
displayProperties()

然后,您将被迫为所有类型的水果实现该方法,然后在for循环中只需调用
displayProperties()

  • 如果要对水果进行排序,需要实现水果类的可比较接口

  • 如果您想显示属性,在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);
      }
     }
    
  • 如果要对水果进行排序,需要实现水果类的可比较接口

  • 如果您想显示属性,在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);
      }
     }
    

  • 不建议这样做,但您可以使用

        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);
        }
    
    }
    
    但是你应该做的是创建一个水果界面,让你可以访问与所有水果相关的重要信息,一般来说,你不应该为了获得额外的信息而放弃

    虽然向下广播不是坏事,但它可能意味着您没有充分利用多态性。要利用多态性,应确保子类型共享的超类型具有获取所需信息或行为的方法

    例如,假设您有一个家养宠物的列表,家养宠物由狗和猫的子类型组成。您在该列表上循环,希望摆动家养宠物可以摆动尾巴的所有尾巴。为简单起见,有两种方法可以做到这一点,或者只有狗有“wagTail()”方法(因为猫不摆动尾巴),或HousePets有一个名为“wagTail()”的方法,该方法由猫和狗继承(但猫的版本不起任何作用)。如果我们选择第一个示例,代码将如下所示:

    if(object instanceof Class) {
        Class temp = (Class) object
        //operate on object
    }
    
     for(HousePet pet : petList)
         if(pet instanceof Dog) {
             ((Dog)pet).wagTail();
         }
    
    第二个例子是这样的:

    if(object instanceof Class) {
        Class temp = (Class) object
        //operate on object
    }
    
     for(HousePet pet : petList)
         if(pet instanceof Dog) {
             ((Dog)pet).wagTail();
         }
    

    第二个例子的代码少了一点,简化了一点。在最坏的情况下,如果我们使用第一个选项,我们将需要为每个新的HousePet子类型使用if块,这将使代码更大/更丑/等等。更好的是,我们可以有一个名为“showHappiness”的HousePet方法,猫发出咕噜声时,狗会摇尾巴。

    不建议这样做,但您可以使用

        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);
        }
    
    }
    
    但是你应该做的是创建一个水果界面,让你可以访问与所有水果相关的重要信息,一般来说,你不应该为了获得额外的信息而放弃

    虽然向下广播不是坏事,但它可能意味着您没有充分利用多态性。要利用多态性,应确保子类型共享的超类型具有获取所需信息或行为的方法

    例如,假设您有一个家养宠物的列表,家养宠物由狗和猫的子类型组成。您在该列表上循环,希望摆动家养宠物可以摆动尾巴的所有尾巴。为简单起见,有两种方法可以做到这一点,或者只有狗有“wagTail()”方法(因为猫不摆动尾巴),或HousePets有一个名为“wagTail()”的方法,该方法由猫和狗继承(但猫的版本不起任何作用)。如果我们选择第一个示例,代码将如下所示:

    if(object instanceof Class) {
        Class temp = (Class) object
        //operate on object
    }
    
     for(HousePet pet : petList)
         if(pet instanceof Dog) {
             ((Dog)pet).wagTail();
         }
    
    第二个例子是这样的:

    if(object instanceof Class) {
        Class temp = (Class) object
        //operate on object
    }
    
     for(HousePet pet : petList)
         if(pet instanceof Dog) {
             ((Dog)pet).wagTail();
         }
    

    第二个例子的代码少了一点,简化了一点。在最坏的情况下,如果我们使用第一个选项,我们将需要为每个新的HousePet子类型使用if块,这将使代码更大/更丑/等等。更好的是,我们可以有一个名为“showHappiness”的HousePet方法,当猫发出呼噜声时,狗会摇尾巴。

    我想你需要一个
    操作符实例。它允许你检查对象类型

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

    我想您需要一个
    instanceof
    操作符。它允许您检查对象类型

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

    您尝试过什么?已经有几十个问题询问如何对对象列表进行排序。将来,请尝试粘贴代码,这样就不会出现语法错误(尤其是缺少paren)。这会让其他人不那么烦人,也会更快。您尝试过什么?已经有几十个问题在问如何对对象列表进行排序。将来,请尝试粘贴代码,这样就不会出现语法错误(尤其是缺少paren)。这会让其他人不那么烦人,也会更快。当然,你需要实现compareTo功能,我没有这么做。当然,你需要实现compareTo功能,我没有这么做。嗨,你能解释一下为什么不推荐这种方法吗?嗨,你能解释一下为什么不推荐这种方法吗?