简体   繁体   English

Java 8 - 两个接口包含具有相同方法签名但返回类型不同的默认方法,如何覆盖?

[英]Java 8 -Two interfaces contain default methods with the same method signature but different return types, how to override?

I understand that if a class implements multiple interfaces containing default methods of same name, then we need to override that method in the child class so as to explicitly define what my method will do. 我理解如果一个类实现包含同名默认方法的多个接口,那么我们需要在子类中重写该方法,以便明确定义我的方法将执行的操作。
Problem is, see the below code : 问题是,见下面的代码:

interface A {
    default void print() {
        System.out.println(" In interface A ");
    }
}

interface B {
    default String print() {
        return "In interface B";
    }
}

public class C implements A, B {

    @Override
    public String print() {
        return "In class C";
    }

    public static void main(String arg[]) {
        // Other funny things
    }
}

Now interface A and B both have a default method with name 'print' but I want to override the print method of interface B - the one that returns a string and leave A's print as is. 现在,接口A和B都有一个名为'print'的默认方法,但我想覆盖接口B的print方法 - 返回字符串并按原样保留A的打印方式。 But this code doesn't compile giving this : 但是这段代码不能编译给出:

Overrides A.print
The return type is incompatible with A.print()

Clearly compiler is trying to override A's print method, and I have no idea why ! 很明显,编译器试图覆盖A的打印方法,我不明白为什么!

This is not possible. 这是不可能的。

8.4.8.3 : 8.4.8.3

If a method declaration d 1 with return type R 1 overrides or hides the declaration of another method d 2 with return type R 2 , then d 1 must be return-type-substitutable for d 2 , or a compile-time error occurs. 如果具有返回类型R 1的方法声明d 1覆盖或隐藏具有返回类型R 2的另一方法d 2的声明,则d 1必须是d 2 return-type-substitutable ,否则发生编译时错误。

8.4.5 : 8.4.5

A method declaration d 1 with return type R 1 is return-type-substitutable for another method d 2 with return type R 2 iff any of the following is true: 返回类型为R 1的方法声明d 1返回类型 - 可替代另一个方法d 2 ,返回类型为R 2 iff以下任何一个为真:

  • If R 1 is void then R 2 is void . 如果R 1 voidR 2 void

  • If R 1 is a primitive type then R 2 is identical to R 1 . 如果R 1是基元类型,则R 2R 1相同。

  • If R 1 is a reference type then one of the following is true: 如果R 1是引用类型,则以下之一为真:

    • R 1 , adapted to the type parameters of d 2 , is a subtype of R 2 . 适合于d 2的类型参数的R 1R 2的子类型。

    • R 1 can be converted to a subtype of R 2 by unchecked conversion. 通过未经检查的转换,可以将R 1转换为R 2的子类型。

    • d 1 does not have the same signature as d 2 , and R 1 = |R 2 | d 1d 2不具有相同的特征,并且R 1 = |R 2 | .

In other words, void , primitive- and reference-returning methods may only override and be overridden by methods of that same respective category. 换句话说, void ,primitive和reference-returns方法可能只会被相同的相应类别的方法覆盖和覆盖。 A void method may only override another void method, a reference-returning method may only override another reference-returning method, and so on. void方法可能只覆盖另一个void方法,引用返回方法可能只覆盖另一个引用返回方法,依此类推。

One possible solution to the problem you're having could be to use composition instead of inheritance: 您遇到的问题的一种可能解决方案可能是使用组合而不是继承:

class C {
    private A a = ...;
    private B b = ...;
    public A getA() { return a; }
    public B getB() { return b; }
}

暂无
暂无

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

相关问题 在 Java 中使用相同签名的两个默认方法实现两个接口 8 - Implementing two interfaces with two default methods of the same signature in Java 8 两个接口中具有相同签名但返回类型不同的方法 - Methods with the same signature but different return type in two interfaces 使用相同的方法签名但不同的返回类型实现两个接口 - Implement two interfaces with the same method signature but different return type Java类有两个具有相同函数签名但返回类型不同的方法 - Java class has 2 methods with the same function signature but different return types Java - 使用相同的方法和不同的返回类型实现多个接口 - Java - implementing multiple interfaces with same method and different return types 两个接口指定具有相同签名的方法,但指定具有不同的行为? - Two interfaces specify methods with the same signature, but specified to have different behavior? 如果两个接口包含相同的默认方法会发生什么? - What happens, if two interfaces contain the same default method? 具有相同方法签名的两个接口在Java类中实现 - Two interfaces with same method signature implemented in Java class 如何为同一方法传递不同的 object 类型,而不是在 java 中编写具有相同功能和返回类型的两个方法 - How to pass different object type for same method instead of writing two methods with same functionality and return type in java 实现从具有不同返回类型的两个接口继承的类方法 - Implement a class method inherited from two interfaces with different return types
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM