简体   繁体   English

实现从具有不同返回类型的两个接口继承的类方法

[英]Implement a class method inherited from two interfaces with different return types

I have two interfaces and one class which implements those two interfaces : 我有两个接口和一个实现这两个接口的类:

interface A {
    int test();
}

interface B {
    void test();
}

class X implements A, B {
    public void test()  { // ==> Error

    }

    public int test() { // ==> Error
        return 10;
    }
}

Why can't i implement method "test" in my class X ? 为什么我不能在我的X类中实现方法“test”?

NB : this case cannot be correct by design or by logic, but i want to know what shall i do in this case, because i'm in the middle of learning java language, and i want to understand this case :) 注意 :这种情况不能通过设计或逻辑来纠正,但我想知道在这种情况下我该怎么做,因为我正在学习java语言,我想了解这种情况:)

The return type alone isn't enough for the compiler to discern between one method or another. 单独的返回类型不足以使编译器在一种方法或另一种方法之间进行辨别。 Consider code which calls that method: 考虑调用该方法的代码:

instanceOfX.test();

Which one should be invoked? 应该调用哪一个? void because the result isn't being assigned? void因为结果未分配? What if I wanted to call the int method but just don't care about the result? 如果我想调用int方法但不关心结果怎么办?

The method signature itself (name and parameters) needs to be different for the compiler to discern one from the other. 方法签名本身(名称和参数)需要不同,以便编译器能够辨别另一个。

Return type doesn't come in method signature. 返回类型不在方法签名中。 A method signature comprise of: 方法签名包括:

  • Method Name 方法名称
  • Format Parameters 格式参数
  • Type Parameters if any. 输入参数(如果有)。

Since both the methods have same name and no parameters, they have same signature. 由于这两种方法具有相同的名称和参数,因此它们具有相同的签名。 And you can't have two methods with same signature in a class. 并且您不能在类中使用两个具有相同签名的方法。

You can only resolve this issue by changing either of the 3 things about that method. 您只能通过更改有关该方法的3件事来解决此问题。

because return type isn't enough to differentiate two method signature, 因为返回类型不足以区分两个方法签名,

resolution: 解析度:

either change method name, or argument list 要么更改方法名称,要么更改参数列表

One cannot have multiple methods of the same signature in the same class, and the signature consists of the name of the method and its parameter types. 在同一个类中不能有多个相同签名的方法,签名由方法名称及其参数类型组成。

To have X implement both A and B , one of the methods in A or B needs to have a different name and/or signature, OR they need to have the same return type and one test method would be enough. 要让X实现ABAB中的A方法需要具有不同的名称和/或签名,或者它们需要具有相同的返回类型,并且一个test方法就足够了。

If you really get this problem in real life, and you can't modify A or B , then I think I'd have X implement just one of those, and define a new method in X to return the other: 如果你真的在现实生活中遇到这个问题,并且你不能修改AB ,那么我认为我只有X实现其中之一,并在X定义一个新方法来返回另一个:

class X implements A {
    public int test() { 
        return 10;
    }

    private void testForB() {
        // do whatever you'd do to implement B's "void test()"
    }

    public B asB() {
        return new B () {
            @Override
            public void test() {
                testForB();
            }
        };
    }

}

Then any client that has an object x of type X , and that needs to use it as a B , would have to say x.asB() . 然后任何具有类型X的对象x且需要将其用作B客户端都必须说x.asB() Not ideal, perhaps, but it would be a way to make things work. 也许并不理想,但这会让事情变得有效。

However, I think that this problem is unlikely to show up in real life. 但是,我认为这个问题不太可能出现在现实生活中。

暂无
暂无

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

相关问题 使用相同的方法签名但不同的返回类型实现两个接口 - Implement two interfaces with the same method signature but different return type 当一个类实现两个接口时,接口具有相同的方法名称,但返回类型不同,为什么它不起作用? - When a class implements two interfaces, interfaces have same method name but different return type why it wont work? Java 8 - 两个接口包含具有相同方法签名但返回类型不同的默认方法,如何覆盖? - Java 8 -Two interfaces contain default methods with the same method signature but different return types, how to override? List的Java泛型返回从多个接口继承的方法的类型 - Java generics in List return type on a method inherited from multiple interfaces Java - 使用相同的方法和不同的返回类型实现多个接口 - Java - implementing multiple interfaces with same method and different return types 如何从方法中返回两种不同类型的值? - How to return two different types of values from a method? 根据同一方法的条件返回两种不同的数据类型 - Return two different data types based on condition from the same method 在匿名类中实现两个接口 - Implement two interfaces in an anonymous class 如何从不同的接口实现相同的接口方法 - Retrofit 和 Twitter - How to implement same Interface method from different Interfaces - Retrofit and Twitter Class 从两个不同的接口继承相同的方法和一个默认实现不会编译 - Class inheriting same method from two different interfaces and a single default implementation won't compile
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM