简体   繁体   English

具有相同方法签名的两个接口在Java类中实现

[英]Two interfaces with same method signature implemented in Java class

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();
    }
}

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 class Tuna has a name, signature, and return type that matches the method declared in interface Fish and also matches the method declared in interface Piano ; Tuna的方法getNumberOfScales有一个名称,签名和返回类型,它与接口Fish声明的方法匹配,并且还匹配接口Piano声明的方法; it is considered to implement both. 它被认为是实现两者。

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. 然后,文本继续注意,如果方法签名具有不同的返回类型,例如doubleint ,则无法在同一个类中实现这两个接口,并且将产生编译时错误。

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. 因此,如果您需要一个实现“DVDPlayer”的类(因为您需要方法“play()”),您将找到CarPlayer。 Same goes for the need of a Class implementing CassettePlayer. 同样需要一个实现CassettePlayer的Class。 That's the technical explanation. 这是技术解释。

But of course in your semantic coding you should ensure that CarPlayer's method "play()" satisfies the semantics of both DVDPlayer and CassettePlayer. 但是当然在你的语义编码中你应该确保CarPlayer的方法“play()”满足DVDPlayer和CassettePlayer的语义。 I think in a practical application it will be a bad practice. 我认为在实际应用中这将是一个不好的做法。

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. 更实际的是,您应该使用方法“play()”创建一个“Player”接口,并且还有另外两个更具体的接口DVDPlayer和CassettePlayer(具有DVD和磁带的特定方法),它们继承自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. 另外,如果你不需要DVD或磁带的特定方法,那么你不需要两个不同的接口只实现一个相同的方法 - 只需使用一个接口播放器,这就足够了。

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() 编译器说:Exception IOException与Plants.eat()中的throws子句不兼容

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

没有冲突,因为它们都指定了相同的契约,实现类只提供了一个通过任一接口引用时调用的方法。

Why not? 为什么不? The class is satisfying the contracts defined by both interfaces. 该类满足两个接口定义的合同。

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. 当然,在可能导致意外行为的更复杂场景中应该避免这种情况。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java 8 - 两个接口包含具有相同方法签名但返回类型不同的默认方法,如何覆盖? - Java 8 -Two interfaces contain default methods with the same method signature but different return types, how to override? 具有相同方法的多个接口,最终由类实现 - multiple interfaces having same method that is finally implemented by class 在 Java 中使用相同签名的两个默认方法实现两个接口 8 - Implementing two interfaces with two default methods of the same signature in Java 8 使用相同的方法签名但不同的返回类型实现两个接口 - Implement two interfaces with the same method signature but different return type Class 从多个具有相同方法签名的接口继承 - Class inheriting from several Interfaces having same method signature 类使用相同的方法实现两个接口,但不同的检查异常 - Class implements two interfaces with the same method but different checked exceptions Java - 具有相同方法的两个接口,一个只是通用的 - Java - two interfaces with same method, one just generic 当一个类实现两个接口时,接口具有相同的方法名称,但返回类型不同,为什么它不起作用? - When a class implements two interfaces, interfaces have same method name but different return type why it wont work? 参数是具有已实现接口的方法 - argument is method with implemented interfaces 接口和类中的方法签名相同 - same method signature in interface and class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM