我有一个父类(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/