java Java类中实现的两个具有相同方法签名的接口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9863835/
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
Two interfaces with same method signature implemented in Java class
提问by Rakesh Waghela
I have two Java interfaces and one implementing class.
我有两个 Java 接口和一个实现类。
(I have used Eclipse to run the program directly, and I did not try to check any compiler warning et cetera by explicitly compiling from the command line.)
(我已经使用 Eclipse 直接运行程序,并且我没有尝试通过从命令行显式编译来检查任何编译器警告等。)
Why do they run without problem? Why does Java allow this, even when it satisfies the "contract" of both interfaces but create ambiguity in implementing class?
为什么它们可以毫无问题地运行?为什么 Java 允许这样做,即使它满足两个接口的“契约”但在实现类时会产生歧义?
Updated the example.
更新了示例。
public interface CassettePlayer {
void play();
}
public interface DVDPlayer {
void play();
}
public class CarPlayer implements CassettePlayer,DVDPlayer{
@Override
public void play() {
System.out.println("This plays DVD, screw you Cassette !");
}
public static void main(String args[]) {
CarPlayer cp = new CarPlayer();
cp.play();
CassettePlayer firstInterface = new CarPlayer();
firstInterface.play();
DVDPlayer secondInterface = new CarPlayer();
secondInterface.play();
}
}
回答by Joni
This scenario specifically allowed in the Java Language Specification, section 8.1.5:
Java 语言规范的第 8.1.5 节特别允许这种情况:
It is permitted for a single method declaration in a class to implement methods of more than one superinterface. For example, in the code:
interface Fish { int getNumberOfScales(); } interface Piano { int getNumberOfScales(); } class Tuna implements Fish, Piano { // You can tune a piano, but can you tuna fish? int getNumberOfScales() { return 91; } }
the method
getNumberOfScales
in classTuna
has a name, signature, and return type that matches the method declared in interfaceFish
and also matches the method declared in interfacePiano
; it is considered to implement both.
允许一个类中的单个方法声明实现多个超接口的方法。例如,在代码中:
interface Fish { int getNumberOfScales(); } interface Piano { int getNumberOfScales(); } class Tuna implements Fish, Piano { // You can tune a piano, but can you tuna fish? int getNumberOfScales() { return 91; } }
getNumberOfScales
class 中的方法Tuna
具有与 interface 中Fish
声明的方法匹配并且也与interface 中声明的方法匹配的名称、签名和返回类型Piano
;它被认为是同时实施的。
The text then goes on to note that if the method signatures had different return types, such as double
and int
, there would be no way to implement both interfaces in the same class and a compile time error would be produced.
然后文本继续指出,如果方法签名具有不同的返回类型,例如double
and int
,则无法在同一类中实现两个接口,并且会产生编译时错误。
回答by Jana
For this issue it's necessary to understand what interfaces are for.
对于这个问题,有必要了解接口的用途。
An interface is a kind of "contract" so that one knows which methods are compulsorily implemented in a Class with that interface.
接口是一种“契约”,以便人们知道在具有该接口的类中强制实现了哪些方法。
So if you need a Class implementing "DVDPlayer" (because you need the method "play()"), you'll find CarPlayer. Same goes for the need of a Class implementing CassettePlayer. That's the technical explanation.
因此,如果您需要一个实现“DVDPlayer”的类(因为您需要方法“play()”),您将找到 CarPlayer。同样需要一个实现 CassettePlayer 的类。这就是技术解释。
But of course in your semantic coding you should ensure that CarPlayer's method "play()" satisfies the semantics of both DVDPlayer and CassettePlayer. I think in a practical application it will be a bad practice.
但是当然在您的语义编码中,您应该确保 CarPlayer 的方法“play()”满足 DVDPlayer 和 CassettePlayer 的语义。我认为在实际应用中这将是一个不好的做法。
Of course in your example it's a bad idea to have two interfaces declaring the same method. More practically, you should have made an interface "Player" with method "play()" and have two other, more specific interfaces DVDPlayer and CassettePlayer (with specific methods for DVDs and cassettes) who inherit from Player. On the onther hand, if you don't need specific methods for DVDs or cassettes, then you don't need two different interfaces only implementing one and the same method - just use one interface Player, that'll be enough.
当然,在您的示例中,让两个接口声明相同的方法是一个坏主意。更实际地,您应该使用方法“play()”创建一个接口“Player”,并有两个从 Player 继承的更具体的接口 DVDPlayer 和 CassettePlayer(具有用于 DVD 和磁带的特定方法)。另一方面,如果您不需要 DVD 或磁带的特定方法,那么您不需要两个不同的接口,只实现一个相同的方法 - 只需使用一个接口 Player,就足够了。
回答by harun ???k
In this situation, there is no issue because both interfaces have same method signature. But What about this ?
在这种情况下,没有问题,因为两个接口具有相同的方法签名。但这又如何呢?
interface Animal {
public void eat() throws IOException;
}
interface Plants {
public void eat() throws NullPointerException;
}
Which one is choosen by the compiler ? Why does it get error below code ?
编译器选择哪一个?为什么在代码下方出现错误?
public class Test implements Animal, Plants {
public void eat() throws IOException {
}
}
Compiler says : Exception IOException is not compatible with throws clause in Plants.eat()
编译器说:异常 IOException 与 Plants.eat() 中的 throws 子句不兼容
回答by user2429311
The following page contains an example of a class that implements two interfaces that have the
以下页面包含一个类的示例,该类实现了两个接口,这些接口具有
1) same variable name 2) same method in each interface.
1) 相同的变量名 2) 每个接口中的相同方法。
http://www.j2eeonline.com/java-tm-fundamentals-II/module2/interface-ambiguous-fields.jsp
http://www.j2eeonline.com/java-tm-fundamentals-II/module2/interface-ambiguous-fields.jsp
回答by blank
There is no conflict because they both specify the same contract, implementing classes only provide the one method which is called when referenced via either interface.
没有冲突,因为它们都指定了相同的契约,实现类只提供一个方法,当通过任一接口引用时调用该方法。
回答by Bozho
Why not? The class is satisfying the contracts defined by both interfaces.
为什么不?该类满足两个接口定义的契约。
回答by Drew Gibson
The class implements both interfaces - so no issue. Of course, this sort of thing should be avoided in more complex scenarios where unintended behaviour might result.
该类实现了两个接口 - 所以没问题。当然,在可能导致意外行为的更复杂的场景中,应该避免这种事情。