java - 我很难弄清楚如何覆盖父类(super class)中的部分 "toString"方法(多态性)

标签 java

我有一个父类(super class)“Vessel”和一个子类“Bottle”。子类“Bottle”进一步有两个子类“GlasBottle”和“PlasticBottle”。

我想要以下输出:

This vessel has a volume of 1

This bottle has a volume of 1 and contains juice

This green glas bottle has a volume of 2 and contains juice

This white glas bottle has a volume of 1 and contains beer

This OTHER bottle has a volume of 2 and contains cola

This PET bottle has a volume of 1 and contains milk

但是,我在覆盖每个类中的不同 toString 方法时遇到问题。

这是我得到的输出:

This vessel has a volume of 1

This bottle This vessel has a volume of 1 and contains juice

This green glas bottle This bottle This vessel has a volume of 2 and contains juice

This white glas bottle This bottle This vessel has a volume of 1 and contains beer

This OTHER bottle This bottle This vessel has a volume of 2 and contains cola

This PET bottle This bottle This vessel has a volume of 1 and contains milk

正如您从第二个输出中看到的那样,它插入了“This vessel”,因为该字符串是我正在调用的“toString”方法的一部分,所以它在那里是有道理的。但是,我希望 java 在我调用 super.toString() 时省略父类(super class)的那部分,只包含其他必要的部分。这是我的父类(super class)和子类:

public class Vessel {

    private int volume=0;

    public Vessel(int volume) {
        this.volume = volume;
    }

    public String toString() {
    return "This vessel has a volume of "+volume;
    }

}

public class Bottle extends Vessel {

    private String content="";

    public Bottle(int volume, String content) {
        super(volume);
        this.setContent(content);
    }

    public String getContent(String content) {
        return this.content = content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String toString() {
        return "This bottle "+super.toString()+" and contains "+content;
    }

}

public class GlasBottle extends Bottle {

    private String color="";

    public GlasBottle(int volume, String content, String color) {
        super(volume, content);
        this.setColor(color);
    }

    public String getColor(String color) {
        return this.color = color;
    }

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

    public String toString() {
        return "This "+color+" glas bottle "+super.toString();
    }

}

public class PlasticBottle extends Bottle {

    private String material="";

    public PlasticBottle(int volume, String content, String material) {
        super(volume, content);
        if(material != "PET") {
            this.material = "OTHER";
        }else {
            this.setMaterial(material);
        }
    }

    public String getMaterial(String material) {
        return this.material = material;
    }

    public void setMaterial(String material) {
        this.material = material;
    }

    public String toString() {
        return "This "+material+" bottle "+super.toString();
    }

}

最佳答案

您想要的是“覆盖”,而不是覆盖。

首先,将 getVolume() 方法添加到您的顶级 Vessel 类,因为您的 volume私有(private)的.

您可以简单地将更改的部分作为单独的方法。 所以在 Bottle 中你放了一个像这样的方法:

protected String getBottleType() {
  return "bottle";
}

并像这样更改您的 toString():

public String toString() {
    return "This " + getBottleType() + " has a volume of " + getVolume() + " and contains " + content;
}

然后在 Bottle 的每个子类中,您只需覆盖 getBottleType()。例如,在 GlassBottle 中,您只需执行以下操作:

@Override
protected String getBottleType() {
 return color + " glass bottle";
}

然后您实际上可以省略子类的 toString() 除非您希望更改句子。

关于java - 我很难弄清楚如何覆盖父类(super class)中的部分 "toString"方法(多态性),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53419766/

相关文章:

java - 如何在枚举中添加特殊字符?

java - 在多线程环境中使用 HttpClient 的最佳实践

java - 无法检索 MongoDB 中的现有文档

java.lang.IllegalStateException 每当我尝试将值添加到填充的 RealmList<MyObject>

java - 在 JBoss 5 上更改 war 文件中的 HTML、JS 和 CSS 等静态内容,无需重新部署

java.lang.ClassNotFoundException : net. sourceforge.jtds.jdbc.Driver 问题

java - PostFix 评估结果错误

java - C#和Java中的SHA512不同

java - 如何在 Android TO 上强制应用程序图标? (防止发射器采用它)

java - 在hadoop编程中,分区,比较在哪里?