简体   繁体   English

Java - 使用相同的方法和不同的返回类型实现多个接口

[英]Java - implementing multiple interfaces with same method and different return types

Consider the following code: 请考虑以下代码:

public interface A {
  public A another();
}

public interface B {
  public B another();
}

public interface AB extends A,B {
  public AB another();
}

This leads to a compile error on AB : 这导致AB上的编译错误:

types B and A are incompatible; B型和A型不兼容; both define another(), but with unrelated return types 两者都定义了另一个(),但具有不相关的返回类型

I've seen this SO question , and follow the incompatibility example in the the accepted answer - ie 我已经看到了这个问题 ,并按照接受的答案中的不兼容性示例 - 即

public interface C { 
  public void doSomething();
}

public interface D {
  public boolean doSomething();
}

public interface CD extends C,D { 
}

However, in that case the return types were genuinely incompatible - a return type cannot be both void and a boolean. 但是,在这种情况下,返回类型实际上是不兼容的 - 返回类型不能同时为void和布尔值。 Whereas, in my example above, the another() return type of AB is both an A and a B , so it is possible to implement both of the extended interfaces. 然而,在上面的例子中, ABanother()返回类型既是A又是B ,因此可以实现两个扩展接口。

Furthermore, having looked at the JLS (8.4.8, 8.4.8.3, 8.4.8.4), I don't quite understand why my example above illegal. 此外,看过JLS(8.4.8,8.4.8.3,8.4.8.4)后,我不太明白为什么我的上面的例子非法。 Can anyone explain this to me? 任何人都可以向我解释这个吗?

Second, are there any solutions/workarounds to this other than repeating the contract requirements of A or B in AB ? 其次,除了重复ABAB的合同要求外,还有其他解决方案/解决方法吗?

This error message appears for pre 1.5 versions of Java (at least I can reproduce the error when setting the compliance level to 1.4 in Eclipse). 对于1.5之前版本的Java,会出现此错误消息(至少我可以在Eclipse中将合规性级别设置为1.4时重现该错误)。 In other words, make sure you're looking at old-enough specs. 换句话说,确保你正在看旧的规格。

On Java >= 1.5 the following compiles fine. 在Java> = 1.5时,以下编译正常。

interface A {
    public A another();
}

interface B {
    public B another();
}

interface AB extends A,B {
    public AB another();
}

As you say, since AB is both an A and a B , it satisfies both interfaces. 如你所说,由于AB既是A又是B ,它满足两个接口。


Here's a quote from the Java Language Specification (Second Edition, ie Java 1.4): 这是Java语言规范(第二版,即Java 1.4)的引用:

9.2 Interface Members 9.2接口成员

The members of an interface are: 接口的成员是:

  • Those members declared in the interface. 那些成员在界面中声明。
  • Those members inherited from direct superinterfaces . 这些成员继承自直接超级接口
  • If an interface has no direct superinterfaces, [...] 如果接口没有直接的超级接口,[...]

It follows that it is a compile-time error if the interface declares a method with the same signature and different return type or incompatible throws clause. 因此,如果接口声明具有相同签名和不同返回类型或不兼容throws子句的方法 ,则它是编译时错误。

Further more, the current spec says the following: 此外, 目前的规范说明如下:

9.4.2 Overloading 9.4.2超载

If two methods of an interface (whether both declared in the same interface, or both inherited by an interface, or one declared and one inherited) have the same name but different signatures that are not override-equivalent (§8.4.2), then the method name is said to be overloaded. 如果接口的两个方法(无论是在同一个接口中声明,还是由接口继承,或者声明的和继承的)都具有相同的名称但不同的签名不等于覆盖(第8.4.2节),那么方法名称被称为重载。 This fact causes no difficulty and never of itself results in a compile-time error. 这个事实没有任何困难,从来没有导致编译时错误。 There is no required relationship between the return types or between the throws clauses of two methods with the same name but different signatures that are not override-equivalent. 返回类型之间或两个方法的throws子句之间没有必需的关系,这两个方法具有相同的名称但不同的签名不是覆盖等效的。

声明:本站的技术帖子网页,遵循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? java 和实现不同参数类型的接口 - java and interfaces implementing different parameter types 使用相同的方法签名实现多个接口 - Implementing multiple interfaces with same method signatures 实现具有相同方法的多个接口 - Implementing multiple interfaces having same method 用不同的泛型类型实现同一泛型Java接口的多个实例? - Implementing multiple instances of the same generic Java interface with different generic types? Java接口和返回类型 - Java interfaces and return types 了解Java中的泛型语法-将多个接口实现为参数的泛型类型 - Understanding generics syntax in java - generic types implementing multiple interfaces as parameters 如何在多个类中抽象出具有不同返回类型的相同方法 - How to abstract out same method with different return types in multiple classes 在java中从具有不同数据类型的单个方法返回多个变量 - Return multiple variables from single method with different data types in java 实现从具有不同返回类型的两个接口继承的类方法 - Implement a class method inherited from two interfaces with different return types
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM