[英]Java 8 -Two interfaces contain default methods with the same method signature but different return types, how to override?
I understand that if a class implements multiple interfaces containing default methods of same name, then we need to override that method in the child class so as to explicitly define what my method will do. 我理解如果一个类实现包含同名默认方法的多个接口,那么我们需要在子类中重写该方法,以便明确定义我的方法将执行的操作。
Problem is, see the below code : 问题是,见下面的代码:
interface A {
default void print() {
System.out.println(" In interface A ");
}
}
interface B {
default String print() {
return "In interface B";
}
}
public class C implements A, B {
@Override
public String print() {
return "In class C";
}
public static void main(String arg[]) {
// Other funny things
}
}
Now interface A and B both have a default method with name 'print' but I want to override the print method of interface B - the one that returns a string and leave A's print as is. 现在,接口A和B都有一个名为'print'的默认方法,但我想覆盖接口B的print方法 - 返回字符串并按原样保留A的打印方式。 But this code doesn't compile giving this : 但是这段代码不能编译给出:
Overrides A.print
The return type is incompatible with A.print()
Clearly compiler is trying to override A's print method, and I have no idea why ! 很明显,编译器试图覆盖A的打印方法,我不明白为什么!
This is not possible. 这是不可能的。
If a method declaration
d 1
with return typeR 1
overrides or hides the declaration of another methodd 2
with return typeR 2
, thend 1
must be return-type-substitutable ford 2
, or a compile-time error occurs. 如果具有返回类型R 1
的方法声明d 1
覆盖或隐藏具有返回类型R 2
的另一方法d 2
的声明,则d 1
必须是d 2
return-type-substitutable ,否则发生编译时错误。
A method declaration
d 1
with return typeR 1
is return-type-substitutable for another methodd 2
with return typeR 2
iff any of the following is true: 返回类型为R 1
的方法声明d 1
是返回类型 - 可替代另一个方法d 2
,返回类型为R 2
iff以下任何一个为真:
If
R 1
isvoid
thenR 2
isvoid
. 如果R 1
void
则R 2
void
。If
R 1
is a primitive type thenR 2
is identical toR 1
. 如果R 1
是基元类型,则R 2
与R 1
相同。If
R 1
is a reference type then one of the following is true: 如果R 1
是引用类型,则以下之一为真:
R 1
, adapted to the type parameters ofd 2
, is a subtype ofR 2
. 适合于d 2
的类型参数的R 1
是R 2
的子类型。
R 1
can be converted to a subtype ofR 2
by unchecked conversion. 通过未经检查的转换,可以将R 1
转换为R 2
的子类型。
d 1
does not have the same signature asd 2
, andR 1 = |R 2 |
d 1
与d 2
不具有相同的特征,并且R 1 = |R 2 |
. 。
In other words, void
, primitive- and reference-returning methods may only override and be overridden by methods of that same respective category. 换句话说, void
,primitive和reference-returns方法可能只会被相同的相应类别的方法覆盖和覆盖。 A void
method may only override another void
method, a reference-returning method may only override another reference-returning method, and so on. void
方法可能只覆盖另一个void
方法,引用返回方法可能只覆盖另一个引用返回方法,依此类推。
One possible solution to the problem you're having could be to use composition instead of inheritance: 您遇到的问题的一种可能解决方案可能是使用组合而不是继承:
class C {
private A a = ...;
private B b = ...;
public A getA() { return a; }
public B getB() { return b; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.