Java - 使用相同的方法和不同的返回类型实现多个接口 [英] Java - implementing multiple interfaces with same method and different return types

查看:170
本文介绍了Java - 使用相同的方法和不同的返回类型实现多个接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码:

public interface A {
  public A another();
}

public interface B {
  public B another();
}

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

这会导致 AB上的编译错误


B和A类型不兼容;两者都定义了另一个(),但是与
不相关的返回类型

types B and A are incompatible; both define another(), but with unrelated return types

我看到了这个问题,并遵循不兼容性接受的答案中的示例 - 即

I've seen this SO question, and follow the incompatibility example in the the accepted answer - i.e.

public interface C { 
  public void doSomething();
}

public interface D {
  public boolean doSomething();
}

public interface CD extends C,D { 
}

但是,在这种情况下,返回类型真的不兼容 - 返回类型不能同时为void和布尔值。然而,在上面的示例中,另一个()返回类型 AB 都是 A B ,因此可以实现两个扩展接口。

However, in that case the return types were genuinely incompatible - a return type cannot be both void and a boolean. 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.

此外,看过JLS(8.4.8,8.4.8.3,8.4.8.4),我不太明白为什么我上面的例子非法。任何人都可以向我解释这个吗?

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. Can anyone explain this to me?

其次,除了重复 A B AB

推荐答案

对于1.5之前版本的Java,会出现此错误消息(至少我可以在Eclipse中将合规性级别设置为1.4时重现该错误)。换句话说,请确保您正在查看足够旧的规格。

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). In other words, make sure you're looking at old-enough specs.

在Java> = 1.5时,以下编译正常。

On Java >= 1.5 the following compiles fine.

interface A {
    public A another();
}

interface B {
    public B another();
}

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

如你所说,因为 AB 既是 A 又是 B ,它满足两个接口。

As you say, since AB is both an A and a B, it satisfies both interfaces.

以下是Java语言规范(第二版,即Java 1.4)的引用:

Here's a quote from the Java Language Specification (Second Edition, i.e. Java 1.4):


9.2接口成员

接口的成员是:


  • 在界面中声明的那些成员。

  • 那些成员继承自直接超级接口

  • 如果接口没有直接的超接口,[...]

接下来是编译时错误如果接口声明一个方法具有相同的签名和不同的返回类型或不兼容的throws子句。

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.

进一步更多,当前规范说明如下:

Further more, the current spec says the following:


9.4.2 Ov erloading

如果接口的两个方法(无论是在同一个接口中声明,还是由接口继承,或者声明了一个,还是一个继承)都有相同的名称,但不是覆盖等效的不同签名(§8.4.2),然后方法名称被称为重载。这个事实没有任何困难,从来没有导致编译时错误。两个方法的返回类型之间或两个方法的throws子句之间没有必需的关系,这两个方法具有相同的名称但不同的签名不等于覆盖。

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. 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.

这篇关于Java - 使用相同的方法和不同的返回类型实现多个接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆