简体   繁体   English

Java:你怎么称这种多重继承模糊?

[英]Java: how do you call this multiple inheritance ambiguity?

Here's an example using multiple interface inheritance in Java and there's an issue. 这是在Java中使用多接口继承的示例,这是一个问题。

Note that I fully know why there's an issue and this is not the point of my question. 请注意,我完全知道为什么会出现问题,这不是我的问题。 The question is about how you name this particular multiple interface inheritance ambiguity, if there's a name for it. 问题是关于如何命名这个特定的多接口继承歧义,如果它有一个名称。

For example, in C++, the ambiguity that arises when you use multiple implementation inheritance and cannot determine which overridden method to use is called the "diamond problem": 例如,在C ++中,当您使用多个实现继承并且无法确定使用哪个重写方法时出现的歧义称为“钻石问题”:

http://en.wikipedia.org/wiki/Diamond_problem http://en.wikipedia.org/wiki/Diamond_problem

Now once again, I know this is not the same problem here: that's not the point. 再一次,我知道这不是同一个问题:那不是重点。 The point is that a name has been coined in that previous case. 关键是在之前的案例中创造了一个名称。

And I'd like to know if a name exists for the issue I'm about to describe. 而且我想知道我将要描述的问题是否存在名称。

Here's an example of another kind of multiple inheritance, where one interface inherits from two other interfaces that have an incompatible method return type: 下面是另一种多重继承的示例,其中一个接口继承自另外两个具有不兼容方法返回类型的接口:

interface A {
  void a();
  Integer c();
}

interface B {
  void b();
  Long c();
}

interface MI extends A, B {...}

(notice multiple interface inheritance at work using the 'extends' keyword) (使用'extends'关键字注意工作中的多个接口继承)

You cannot do that, because: 你做不到,因为:

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

Has a name been coined to describe that situation? 有没有一个名字来描述这种情况?

I'm not sure there is a specific name for it, or at least it doesn't seem to be very commonly used. 我不确定它是否有特定的名称,或者至少它似乎并不常用。 It's "just" a problem of the implicit mapping of interface methods to class methods; 它只是“接口方法”隐式映射到类方法的问题; if you could have overloads which differ in return types only, there would be no problem either. 如果你可能只有返回类型不同的重载,那么也没有问题。 So it comes down to an signature/overloading/implicit method mapping problem. 因此,它归结为签名/重载/隐式方法映射问题。

In the "Thinking in Java" online book, there isn't a name for it either. 在“Thinking in Java”在线书籍中,也没有名称。 http://www.linuxtopia.org/online_books/programming_books/thinking_in_java/TIJ310_001.htm http://www.linuxtopia.org/online_books/programming_books/thinking_in_java/TIJ310_001.htm

Just a side-note, C# allows explicit interface implementations, which addresses this problem. 只是旁注,C#允许显式接口实现,解决了这个问题。

JLS §6.4.4, The Members of an Interface Type calls such duplicate superinterface members ambiguous , and requires a compile-time error. JLS§6.4.4,接口类型的成员调用此类重复的超级接口成员不明确 ,并且需要编译时错误。 I was hoping for something colorful such as the Beaujolais Effect , Heisenbug , et al . 我希望有一些丰富多彩的东西,比如Beaujolais EffectHeisenbug Maybe two's-a-crowd ? 也许是两个人群

I also don't know of any specific name for this problem. 我也不知道这个问题的具体名称。 Whenever it arised it was described in a sentence containing the words return type incompatibility at some point. 每当它出现时,它在一个句子中描述,其中包含在某些时候返回类型不兼容的单词。 You could also call it the Map/Set incompatibilty as this is one of the more prominent and annoying examples in the Java class libraries. 您也可以将其称为Map / Set不兼容,因为这是Java类库中更为突出和烦人的示例之一。 It makes it impossible to have the same class implement Map as well as Set or Collection just because Map defines a remove(Object) method with a different return type than Collection. 它使得无法使用相同的类实现Map以及Set或Collection,因为Map定义了一个与Collection不同的返回类型的remove(Object)方法。

public interface Collection<E> extends Iterable<E> {
    boolean remove(Object o);
}
public interface Set<E> extends Collection<E> {
}
public interface Map<K,V> {
    V remove(Object key);
}

I'd hesitate to call this a multiple inheritance issue, because interfaces merely describe well, interface--a set of methods an implementing class must define--rather than any implementation. 我不愿意将此称为多继承问题,因为接口只是描述得很好,接口 - 实现类必须定义的一组方法 - 而不是任何实现。 Extending an interface with other interfaces doesn't really mean the subinterface inherits from the superinterface, but rather that the subinterface is, in essence, a concatenation of the methods defined in the two. 扩展与其他接口的接口并不意味着子接口继承自超接口,而是子接口本质上是两者中定义的方法的串联。

If a third interface is used to extend the subinterface and provides a conflicting method declaration, it's essentially the same as if you had just provided the same two conflicting methods in the same interface. 如果使用第三个接口来扩展子接口并提供冲突的方法声明,那么它基本上就像您在同一个接口中提供了相同的两个冲突方法一样。

I don't remember if I have ever seen any name for this. 我不记得我是否见过这个名字。 In Java Language Specification there is no name for this either. Java语言规范中 ,也没有这个名称。

The issue you describe exists in .NET as well as Java, but has a simple solution there: the .NET framework allows a class to implement an interface member using a class member with a different name. 您描述的问题存在于.NET和Java中,但在那里有一个简单的解决方案:.NET框架允许类使用具有不同名称的类成员实现接口成员。 Thus, although the class methods which implement two interface members which differ only in return type are required to have different names, that does not preclude their ability to implement interface members with the same name. 因此,尽管实现仅在返回类型上不同的两个接口成员的类方法需要具有不同的名称,但这并不排除它们实现具有相同名称的接口成员的能力。

If an interface inherits two interfaces with conflicting members, a class which implements the composite interface may implement the members just as if it had inherited the conflicting interfaces directly. 如果接口继承了具有冲突成员的两个接口,则实现组合接口的类可以实现成员,就像它直接继承冲突接口一样。 Consumers of the combined interface will generally not be able to use the members of either component interface without a converting the reference to one of the other interface types, but the cast in question will be considered an upcast rather than an downcast. 组合接口的消费者通常无法使用任一组件接口的成员而不将引用转换为其他接口类型之一,但有问题的转换将被视为向上而不是向下转换。

The scheme implemented in .NET works nicely there. 在.NET中实现的方案在那里很好用。 Unfortunately, there's no way to do anything similar in Java. 不幸的是,在Java中没有办法做类似的事情。 I don't know that Java will squawk if an interface inherits other interfaces with conflicting members, but whether or not it squawks at that point, there would be no way to produce a class which could implement it. 我不知道如果一个接口继承了具有冲突成员的其他接口,Java会发出尖叫声,但是不管它是否在那个时候发出声响,就没有办法产生一个可以实现它的类。

I don't think a name has been defined because interfaces in Java cannot have method implementation, the problem is therefore avoided since there is always only one implementation to a specific method and hence no ambiguity will arise. 我不认为已经定义了名称,因为Java中的接口不能具有方法实现,因此避免了问题,因为对于特定方法总是只有一个实现,因此不会出现歧义。

Have I missed the point or are you talking about the 'c' variable? 我是否错过了这一点,或者你在谈论'c'变量?

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

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