简体   繁体   English

两个接口具有相同的方法名称和不同的返回类型

[英]Two interface have same method name with different return type

This question is for understanding interfaces in Java. 这个问题是为了理解Java中的接口。 Here is a very simple example of implementing a interfaces in Java. 这是在Java中实现接口的一个非常简单的示例。

interface ParentA {
    void display();
}

interface ParentB {
    int display();
}

class Child implements ParentA, ParentB {
    @Override
    public void display() {
        System.err.println("Child ParentA");
    }
    //ERROR : The return type is incompatible with ParentB.display()

    //So added method with int return type too
    @Override
    public int display() {
        System.err.println("Child ParentB");
    }
}

This case can happen in large Java application where two interface can have method with same name. 这种情况可能发生在大型Java应用程序中,其中两个接口可以具有相同名称的方法。 I thought that since return type is different JVM will know which interface's method we are overriding. 我认为,由于返回类型不同,JVM将知道我们覆盖哪个接口的方法。

What is the best explanation for this? 对此最好的解释是什么? Does this situation make sense? 这种情况有意义吗?

Thanks in Advance 提前致谢

Because method with same signature is not allowed, It confuses compiler to detect the exact override-equivalent method from the declared once. 因为不允许使用具有相同签名的方法,所以它会混淆编译器以从声明的一次检测精确的覆盖等效方法。

JLS (§8.4.2) JLS(§8.4.2)

Two methods or constructors, M and N, have the same signature if they have, 两个方法或构造函数M和N如果有,则具有相同的签名,

  • the same name 同名
  • the same type parameters (if any) ( §8.4.4 ), and 相同的类型参数(如果有的话)( §8.4.4 ),和
  • after adapting the formal parameter types of N to the the type parameters of M, the same formal parameter types. 在将N的形式参数类型调整为M的类型参数之后,使用相同的形式参数类型。

It is a compile-time error to declare two methods with override-equivalent signatures in a class. 在类中声明具有覆盖等效签名的两个方法是编译时错误。

If you found yourself in this situation and couldn't solve in a cleaner way, you could have one or two inner classes that forward the calls to newly named methods: 如果您发现自己处于这种情况并且无法以更清晰的方式解决,则可以使用一个或两个内部类来转发对新命名方法的调用:

class Child {
    private class ParentAImp implements ParentA {
      @Override
      public void display() {
          displayParentA();
      }
    }

    private class ParentBImp implements ParentB {
      @Override
      public int display() {
          return displayParentB(); 
      }
    }

    public ParentA asParentA(){ return new ParentAImp(); }
    public ParentB asParentB(){ return new ParentBImp(); }

    private void displayParentA() {
        System.err.println("Child ParentA");
    }

    private int displayParentB() {
        System.err.println("Child ParentB");
        return 0;
    }
}

Drawback is now to get from Child to interface you have to do: 现在回顾从Child到接口你需要做的事情:

ParentA parentA = child.asParentA();
ParentB parentB = child.asParentB();

It is not possible to have in the same class two methods with the same signature and different return type. 在同一个类中不可能有两个具有相同签名和不同返回类型的方法。

Definition of signature: 签字的定义:

The combination of the method name and the parameter list. 方法名称和参数列表的组合。

This happens because it is not possible for the jvm two choose between to methods that differs only from the return type. 发生这种情况是因为jvm两者不可能在仅与返回类型不同的方法之间进行选择。 The jvm infact can investigate only the name of the method and the type of parameters of the call. jvm infact只能调查方法的名称和调用的参数类型。

So your example is not possible in java. 所以你的例子在java中是不可能的。

Probably you have to create two classes, one for interface. 可能你必须创建两个类,一个用于接口。

To implement overriding you are breaking overloading rules. 要实现覆盖,您将打破重载规则。

The conditions for method overloading 方法重载的条件

  1. The number of parameters is different for the methods. 方法的参数数量不同。
  2. The parameter types are different (like changing a parameter that was a float to an int). 参数类型不同(例如将float的参数更改为int)。

So in your case, you can not have same method name having same method arguments (in your case no arguments) and different return type. 因此,在您的情况下,您不能使用相同的方法名称具有相同的方法参数(在您的情况下没有参数)和不同的返回类型。


If you add a parameter to one of your interface method then your code will compile. 如果您向其中一个接口方法添加参数,那么您的代码将被编译。

interface ParentA {
    void display();
}

interface ParentB {
    int display(int x);
}

class Child implements ParentA, ParentB {
    @Override
    public void display() {
        System.err.println("Child ParentA");
    }

    @Override
    public int display(int x) {
        System.err.println("Child ParentB");
        return 0;
    }
}

If you think about it from the side of some code calling methods on an instance of Child: 如果你从一个代码调用Child的一个实例上的方法来考虑它:

Child child = new Child();
child.display();

Which display method would be called? 将调用哪种显示方法? Seeing as it could be either method, you'd see a compile time error. 看到它可能是任何一种方法,您会看到编译时错误。

An interesting way round this in Java 8 would be to use a default method in one of the interfaces; 在Java 8中使用一种有趣的方法是在其中一个接口中使用默认方法; these come with their own set of rules. 这些都有自己的一套规则。

暂无
暂无

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

相关问题 如何创建两个具有相同名称,签名和返回类型的方法的类,就像它们实现相同的接口一样 - How to make two classes which both have method with the same name, signature and return type behave like they would implement the same interface 当一个类实现两个接口时,接口具有相同的方法名称,但返回类型不同,为什么它不起作用? - When a class implements two interfaces, interfaces have same method name but different return type why it wont work? 继承同名但返回类型不同的方法 - Inheriting a method with the same name but different return type 使用具有相同方法但返回类型不同的两个接口的Java多重继承 - Java multiple inheritance using two interface having same method but different return type 从两个不同的接口调用相同的方法名称 - Java - Calling same method name from two different interface - Java 使用相同的方法签名但不同的返回类型实现两个接口 - Implement two interfaces with the same method signature but different return type 相同方法的不同返回类型 - Different return type for the same method 接口名称与其方法参数类型相同 - Interface same name as its method parameter type 如何为同一方法传递不同的 object 类型,而不是在 java 中编写具有相同功能和返回类型的两个方法 - How to pass different object type for same method instead of writing two methods with same functionality and return type in java Extends 和 Interface 具有相同的方法,但具有相同的参数但不同的返回类型 - Extends and Interface has same method with the same parameters but different return types
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM