简体   繁体   English

接口多重继承的冲突方法

[英]Conflicting methods on interface multiple inheritance

I have the following interfaces (in Java, but it's more of an OO question, not language-specific, I'm interested in answers for any language) 我有以下接口(在Java中,但它更多的是OO问题,而不是语言特定的,我对任何语言的答案感兴趣)

public interface A {
    int foo();
}

and

public interface B {
    char foo();
}

If I now want to make the following class: 如果我现在要做以下课程:

public class C implements A,B{
    public int foo() {
        return 0;
    }
    public char foo() {
        return 0;
    }
}

This won't compile because the methods are conflicting. 这将无法编译,因为方法是冲突的。 Is there any way to make this work, or something with the same meaning (of course without modifying A or B, that would be trivial)? 有没有办法让这项工作,或具有相同含义的东西(当然不修改A或B,这将是微不足道的)?

No, the return type cannot be a deciding factor in making a method signature unique because you do not need to assign the returned value to anything, the compiler wouldn't know what to do in that case. 不,返回类型不能成为使方法签名唯一的决定因素,因为您不需要将返回的值分配给任何东西,编译器在这种情况下不知道该怎么做。

Concrete example: 具体例子:

...
C object = new C();
object.foo();
...

Which foo did I just call? 我刚才打电话给哪个foo? Can't tell. 说不出来。

To make this work you'll need to have either different method names or different parameter types in the interface methods. 要完成这项工作,您需要在接口方法中使用不同的方法名称或不同的参数类型。

EDIT: assuming you have no control over the interfaces A and B (library classes or similar) this is the solution I'd take if I wanted to implement them in the same class: 编辑:假设你无法控制接口A和B(库类或类似),这是我想要在同一个类中实现它们的解决方案:

public class C {
    private objectA = new AImpl();
    private objectB = new BImpl();

    // Work with the objects here

    private class AImpl implements A {
        public int foo() {
            // ...
        }
    }

    private class BImpl implements B {
        public char foo() {
            // ...
        }
    }
}

This problem is present because in OOP, is considered the existence of more one method with the same name but with different parameters and not by return type. 出现此问题是因为在OOP中,认为存在多个具有相同名称但具有不同参数而非返回类型的方法。

The problem is not the interface, the problem is the class. 问题不在于界面,问题在于类。

The common solution is to give each method a different name. 常见的解决方案是为每个方法指定一个不同的名称。 Avoid generic names that have a high chance of naming collision with another interface. 避免使用很有可能命名与其他接口冲突的通用名称。

重载带有参数。

There are several solutions to this problem in use. 使用中存在几种解决该问题的方案。 I am working on the assumption that these are independent interfaces, that the intention is multiple interface (implementation of two unrelated interfaces on the same object) and that overloading has nothing to do with it. 我正在假设这些是独立的接口,意图是多个接口(在同一个对象上实现两个不相关的接口),并且重载与它无关。

The solutions I am aware of are: 1. Scoping. 我所知道的解决方案是:1。范围界定。 A reference to foo() can be qualified as A.foo() or B.foo() to determine which is required. 对foo()的引用可以被限定为A.foo()或B.foo()以确定哪个是必需的。 1. Namespaces. 1.命名空间。 The interfaces are inherited inside a namespace constructed for the purpose, and all references to foo() must be preceded by a namespace, eg A::foo(), B::foo(). 接口在为此目的构造的命名空间内继承,并且对foo()的所有引用必须以命名空间开头,例如A :: foo(),B :: foo()。 1. Aliasing. 别名。 One or both of the foo() methods are explicitly renamed when inherited. 在继承时显式重命名了一个或两个foo()方法。 Calls become something like A_foo() and B_foo(). 调用变得像A_foo()和B_foo()。

Ada certainly had a mechanism like this, and I think some variants of Pascal did too. Ada肯定有这样的机制,我认为Pascal的一些变体也是如此。 I can find more examples if it's important. 如果它很重要,我可以找到更多的例子。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM