[英]Implement a class method inherited from two interfaces with different return types
I have two interfaces and one class which implements those two interfaces : 我有两个接口和一个实现这两个接口的类:
interface A {
int test();
}
interface B {
void test();
}
class X implements A, B {
public void test() { // ==> Error
}
public int test() { // ==> Error
return 10;
}
}
Why can't i implement method "test" in my class X ? 为什么我不能在我的X类中实现方法“test”?
NB : this case cannot be correct by design or by logic, but i want to know what shall i do in this case, because i'm in the middle of learning java language, and i want to understand this case :) 注意 :这种情况不能通过设计或逻辑来纠正,但我想知道在这种情况下我该怎么做,因为我正在学习java语言,我想了解这种情况:)
The return type alone isn't enough for the compiler to discern between one method or another. 单独的返回类型不足以使编译器在一种方法或另一种方法之间进行辨别。 Consider code which calls that method:
考虑调用该方法的代码:
instanceOfX.test();
Which one should be invoked? 应该调用哪一个?
void
because the result isn't being assigned? void
因为结果未分配? What if I wanted to call the int
method but just don't care about the result? 如果我想调用
int
方法但不关心结果怎么办?
The method signature itself (name and parameters) needs to be different for the compiler to discern one from the other. 方法签名本身(名称和参数)需要不同,以便编译器能够辨别另一个。
Return type doesn't come in method signature. 返回类型不在方法签名中。 A method signature comprise of:
方法签名包括:
Since both the methods have same name and no parameters, they have same signature. 由于这两种方法具有相同的名称和参数,因此它们具有相同的签名。 And you can't have two methods with same signature in a class.
并且您不能在类中使用两个具有相同签名的方法。
You can only resolve this issue by changing either of the 3 things about that method. 您只能通过更改有关该方法的3件事来解决此问题。
because return type isn't enough to differentiate two method signature, 因为返回类型不足以区分两个方法签名,
resolution: 解析度:
either change method name, or argument list 要么更改方法名称,要么更改参数列表
One cannot have multiple methods of the same signature in the same class, and the signature consists of the name of the method and its parameter types. 在同一个类中不能有多个相同签名的方法,签名由方法名称及其参数类型组成。
To have X
implement both A
and B
, one of the methods in A
or B
needs to have a different name and/or signature, OR they need to have the same return type and one test
method would be enough. 要让
X
实现A
和B
, A
或B
中的A
方法需要具有不同的名称和/或签名,或者它们需要具有相同的返回类型,并且一个test
方法就足够了。
If you really get this problem in real life, and you can't modify A
or B
, then I think I'd have X
implement just one of those, and define a new method in X
to return the other: 如果你真的在现实生活中遇到这个问题,并且你不能修改
A
或B
,那么我认为我只有X
实现其中之一,并在X
定义一个新方法来返回另一个:
class X implements A {
public int test() {
return 10;
}
private void testForB() {
// do whatever you'd do to implement B's "void test()"
}
public B asB() {
return new B () {
@Override
public void test() {
testForB();
}
};
}
}
Then any client that has an object x
of type X
, and that needs to use it as a B
, would have to say x.asB()
. 然后任何具有类型
X
的对象x
且需要将其用作B
客户端都必须说x.asB()
。 Not ideal, perhaps, but it would be a way to make things work. 也许并不理想,但这会让事情变得有效。
However, I think that this problem is unlikely to show up in real life. 但是,我认为这个问题不太可能出现在现实生活中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.