简体   繁体   English

如何从父类而不是超类调用重写的方法?

[英]How to call an overridden method from a parent class and not the super class?

I have an abstract class, Mower, that overrides the toString() method.我有一个抽象类 Mower,它覆盖了 toString() 方法。 The Lawn Tractor class extends Mower.草坪拖拉机类扩展了割草机。 It overrides the toString() method and calls super.toString() as the first line in the method.它覆盖 toString() 方法并调用 super.toString() 作为方法的第一行。 I then have a Commercial class that extends LawnTractor and overrides the toString(), calling super.toString() in its first line as well.然后我有一个 Commercial 类,它扩展了 LawnTractor 并覆盖了 toString(),也在它的第一行调用了 super.toString()。 When instantiate a Commercial object (declared as a Mower object) and call its toString() I expect it to print both properties from Mower and Lawn Tractor and then it's own properties.当实例化一个 Commercial 对象(声明为 Mower 对象)并调用它的 toString() 时,我希望它打印 Mower 和 Lawn Tractor 的两个属性,然后打印它自己的属性。 Instead, it prints only the properties from Mower and then it's own properties.相反,它只打印来自 Mower 的属性,然后打印它自己的属性。 How do I call or override the toString() method in Commercial so that it calls its parent (LawnTractor) toString() and not just the super class (Mower).如何调用或覆盖 Commercial 中的 toString() 方法,以便它调用其父类 (LawnTractor) toString() 而不仅仅是超类 (Mower)。

public abstract class Mower {
   private String manufacturer;
   private int year;
   private String serialNumber;

   public String tostring(){
       String temp = "Manufacturer: " + getManufacturer() + "\nYear: " + getYear() + "\nSerial Number:" + getSerialNumber() + "\n";
       return temp;
   }
}
public class LawnTractor extends Mower{
   private Engine engine = new Engine();
   private String model;
   private double deckWidth;

   public LawnTractor(){
      super();
   }

   public String toString(){
      String temp = super.tostring() + "Engine:\n" + getEngine().toString() + "\nModel: " + getModel() + "\nDeck Width: " + getDeckWidth() + "\n";
      return temp;
}
public class CommercialMower extends LawnTractor {
    private double operatingHours;
    private boolean zeroTurnRadius;

    public CommercialMower(){
       super();
    }
    public String toString(){
       String temp = super.toString() + "Operating Hours: " + getOperatingHours() + "\nZero Turn Radius: " + isZeroTurnRadius() + "\n";
       return temp;
}

So it sounds like you are trying to do is create a hierarchy of toStrings to print when you have a graph of objects.因此,听起来您正在尝试创建一个 toStrings 层次结构,以便在您拥有对象图时进行打印。 So all you need to do is append the super's toStrings as they are called up the chain.所以你需要做的就是在 super 的 toStrings 被调用时附加它们。 (If I'm understanding you correctly) (如果我理解正确的话)

Here's the Mower class这是割草机类

public class Mower {
    @Override
    public String toString() {
        return "Mower toString\n";
    }
}

LawnTractor草坪拖拉机

public class LawnTractor extends Mower {
    @Override
    public String toString() {
        String superString = super.toString();
        return superString + "LawnTractor toString\n";
    }
}

Commercial商业的

public class Commercial extends LawnTractor {
    @Override
    public String toString() {
        String superString = super.toString();
        return superString + "Commercial toString\n";
    }
}

Main class to run it.运行它的主类。 See the output below.请参阅下面的输出。 Is this where you are going with this.这是你要去的地方吗?

public class Main {
    public static void main(String[] args) {
        Mower mow = new Commercial();
        System.out.println(mow.toString());

    }
}

Here's your output.这是你的输出。 I believe this is what you're looking for.我相信这就是你要找的。 If not please clarify如果不是请说明

Mower toString
LawnTractor toString
Commercial toString

The order of how you want the objects to show up in the return string can be reordered depending on how you append them together.您希望对象在返回字符串中显示的顺序可以根据您将它们附加在一起的方式重新排序。 Mower can be first or last.割草机可以是第一个或最后一个。

I realized my mistake.我意识到了我的错误。 My Mower class has tostring() method but does not override the toString() method (difference in syntax).我的割草机类有 tostring() 方法,但没有覆盖 toString() 方法(语法不同)。

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

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