Java - 错误:返回类型不兼容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6015055/
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
Java - Error : return type is incompatible
提问by mike
I'm learning java. I was trying to run the code, where I got this error: return type is incompatible
.
Part of code where it showed me error.
我正在学习java。我试图运行代码,在那里我得到了这个错误:return type is incompatible
。显示错误的部分代码。
class A {
public void eat() { }
}
class B extends A {
public boolean eat() { }
}
Why it is happening?
为什么会发生?
回答by sgokhales
This is because we cannot have two methods in classes that has the same name but different return types.
这是因为我们不能在类中拥有两个名称相同但返回类型不同的方法。
The sub class cannot declare a method with the same name of an already existing method in the super class with a different return type.
子类不能声明与超类中已有方法同名但返回类型不同的方法。
However, the subclass can declare a method with the same signature as in super class. We call this "Overriding".
但是,子类可以声明具有与超类中相同签名的方法。我们称之为“覆盖”。
You need to have this,
你需要有这个
class A {
public void eat() { }
}
class B extends A {
public void eat() { }
}
OR
或者
class A {
public boolean eat() {
// return something...
}
}
class B extends A {
public boolean eat() {
// return something...
}
}
A good practice is marking overwritten methods by annotation @Override
:
一个好的做法是通过注释标记被覆盖的方法@Override
:
class A {
public void eat() { }
}
class B extends A {
@Override
public void eat() { }
}
回答by MarcoS
if B
extends A
then you can override methods (like eat
), but you can't change their signatures. So, your B
class must be
如果B
扩展,A
那么您可以覆盖方法(如eat
),但您不能更改它们的签名。所以,你的B
班级必须是
class B extends A {
public void eat() { }
}
回答by Vincent Cantin
B extends A
should be interpreted as B is a A.
B extends A
应该解释为 B 是 A。
If A's method doesn't return anything, B should do the same.
如果 A 的方法没有返回任何内容,B 也应该这样做。
回答by missingfaktor
When a method in subclass has same name and arguments (their types, number, and order) as the method in superclass then the method in subclass overrides the one in superclass.
当子类中的方法与超类中的方法具有相同的名称和参数(它们的类型、编号和顺序)时,子类中的方法将覆盖超类中的方法。
Now for the overriding to be allowed the return type of the method in subclass must comply with that of the method in superclass. This is possible only if the return type of the method in subclass is covariantwith that of the method in superclass.
现在要允许覆盖,子类中方法的返回类型必须符合超类中方法的返回类型。这只有在子类中方法的返回类型与超类中方法的返回类型协变时才有可能。
Since, boolean </: void
(read: boolean
isn't subtype of void
), compiler raises the "return type incompatible" error.
由于,boolean </: void
(阅读:boolean
不是 的子类型void
),编译器引发“返回类型不兼容”错误。
回答by Vivek Vermani
This is neither overloading nor overriding. We cannot overload on the return type and we cannot override with different different return types ( unless they are covariant returns wef Java 1.5 ).
这既不是重载也不是覆盖。我们不能重载返回类型,也不能用不同的返回类型覆盖(除非它们是 Java 1.5 的协变返回)。