子类中的 toString 不能覆盖抽象 toString ,Java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7730630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
toString in subClasses Cannot Override abstract toString , Java
提问by Mohammad Fadin
I have this very long exercise and I came a cross a problem in each sub class. The problem says and I have no idea what mistake I've made while writing. If you could check the 4 toString methods I would be much apprecaited .
我有这个很长的练习,我在每个子类中都遇到了一个问题。问题说,我不知道我在写作时犯了什么错误。如果您可以检查 4 个 toString 方法,我将非常感激。
The code is here: http://paste.org/pastebin/view/39488I know I should past the code here but it is very long and I'm not able to organize it well.
代码在这里:http: //paste.org/pastebin/view/39488我知道我应该把代码放在这里,但它很长,我无法很好地组织它。
toString() in Shape cannot override toString() in java.lang.Object; attempting to use incompatible return type
toString() in Shape cannot override toString() in java.lang.Object; attempting to use incompatible return type
toString() in Square cannot override toString() in java.lang.Object; attempting to use incompatible return type
`
toString() in Square cannot override toString() in java.lang.Object; attempting to use incompatible return type
`
toString() in Sphere cannot override toString() in java.lang.Object; attempting to use incompatible return type
toString() in Sphere cannot override toString() in java.lang.Object; attempting to use incompatible return type
toString() in Cube cannot override toString() in java.lang.Object; attempting to use incompatible return type
toString() in Cube cannot override toString() in java.lang.Object; attempting to use incompatible return type
thanks
谢谢
回答by Kaivosukeltaja
You need to change the return type of the function to String
and return the text instead of writing it to System.out
.
您需要将函数的返回类型更改为String
并返回文本而不是将其写入System.out
。
public String toString() {
return "(" + super.getX() + ", " +
super.getY() +") " + "side: " + super.getDimension1();
}
EDIT: If you want to have a method that outputs the object directly to System.out
in textual form, you'll need to call it something else than toString()
. This is because toString()
is a method belonging to java.lang.Object
which all Java classes automatically extend.
编辑:如果你想有一个直接以System.out
文本形式输出对象的方法,你需要调用它而不是toString()
. 这是因为toString()
是属于java.lang.Object
所有 Java 类自动扩展的方法。
回答by MasterCassim
toString() has to return String
not void
.
toString() 必须返回String
not void
。
// false
public abstract void toString();
// right
public abstract String toString();
Note: You should not print (System.out) in the toString() method. You should rather return a String represenation of the object.
注意:您不应在 toString() 方法中打印 (System.out)。您应该返回对象的字符串表示。
回答by Cygnusx1
because you try to override it with a void return type. toString should return a String.
因为您尝试使用 void 返回类型覆盖它。toString 应该返回一个字符串。
回答by Mob
It should return a string and not void.
它应该返回一个字符串而不是 void。
public abstract String toString()
回答by Rakesh
toString()
is implemented in Object
class and every class extends it. This method is in every class and we can't have two method with same signature but different return type.
As toString()
is already there with return type String
, we can't have one more toString()
with any other return type.
toString()
在Object
类中实现,每个类都扩展它。这个方法在每个类中,我们不能有两个签名相同但返回类型不同的方法。正如toString()
return type 已经存在的那样String
,我们不能再有toString()
任何其他返回类型。