Java:你如何称呼这种多重继承歧义?

发布于 2024-08-21 16:23:19 字数 745 浏览 1 评论 0 原文

这是一个在 Java 中使用多接口继承的示例,但存在一个问题。

请注意,我完全知道为什么会出现问题,但这不是我问题的重点。问题是如何命名这个特定的多接口继承歧义(如果有一个名称的话)。

例如,在 C++ 中,当您使用多个实现继承并且无法确定要使用哪个重写方法时出现的歧义称为“钻石问题”:

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

现在,我再次知道这不是同一个问题:这不是重点。关键是在之前的案例中已经创造了一个名字。

我想知道我将要描述的问题是否存在一个名称。

这是另一种多重继承的示例,其中一个接口继承自其他两个具有不兼容方法返回类型的接口:(

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

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

interface MI extends A, B {...}

注意使用“extends”关键字进行多重接口继承)

您不能这样做,因为:

A 型和 B 型不兼容;两个都 定义 c() 但返回不相关 类型

是否已经创造了一个名称来描述这种情况?

Here's an example using multiple interface inheritance in Java and there's an issue.

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":

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)

You cannot do that, because:

types A and B are incompatible; both
define c() but with unrelated return
type

Has a name been coined to describe that situation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

囍笑 2024-08-28 16:23:19

我不确定它有一个具体的名称,或者至少它似乎不太常用。这“只是”接口方法到类方法的隐式映射的问题;如果您可以有仅返回类型不同的重载,那么也不会有问题。所以这归结为签名/重载/隐式方法映射问题。

在《Thinking in Java》在线书中,也没有为它命名。
http://www.linuxtopia.org/online_books/programming_books/thinking_in_java/TIJ310_001。 htm

顺便说一句,C# 允许显式接口实现,这解决了这个问题。

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

吻泪 2024-08-28 16:23:19

JLS §6.4.4,接口类型的成员调用此类重复的超级接口成员不明确,并且需要编译时错误。我希望有一些丰富多彩的东西,例如博若莱效应Heisenbug, 。也许两人成群

JLS §6.4.4, The Members of an Interface Type calls such duplicate superinterface members ambiguous, and requires a compile-time error. I was hoping for something colorful such as the Beaujolais Effect, Heisenbug, et al. Maybe two's-a-crowd?

×眷恋的温暖 2024-08-28 16:23:19

我也不知道这个问题的任何具体名称。每当它出现时,都会在某个时刻用包含“返回类型不兼容”一词的句子进行描述。您也可以将其称为映射/设置不兼容性,因为这是 Java 类库中最突出和最烦人的示例之一。仅仅因为 Map 定义了一个与 Collection 不同的返回类型的 remove(Object) 方法,就不可能让同一个类实现 Map 以及 Set 或 Collection。

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

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);
}
执手闯天涯 2024-08-28 16:23:19

我不太愿意将此称为多重继承问题,因为接口只是很好地描述了接口(实现类必须定义的一组方法),而不是任何实现。用其他接口扩展一个接口实际上并不意味着子接口继承自超级接口,而是子接口本质上是两者中定义的方法的串联。

如果使用第三个接口来扩展子接口并提供冲突的方法声明,则其本质上与您刚刚在同一接口中提供相同的两个冲突方法相同。

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.

说好的呢 2024-08-28 16:23:19

我不记得是否见过这个的名字。在 Java 语言规范也没有这个名称。

I don't remember if I have ever seen any name for this. In Java Language Specification there is no name for this either.

横笛休吹塞上声 2024-08-28 16:23:19

您描述的问题在 .NET 和 Java 中都存在,但有一个简单的解决方案:.NET 框架允许类使用具有不同名称的类成员来实现接口成员。因此,虽然实现两个仅返回类型不同的接口成员的类方法需要具有不同的名称,但这并不妨碍它们实现具有相同名称的接口成员的能力。

如果一个接口继承了两个具有冲突成员的接口,则实现复合接口的类可以实现这些成员,就像它直接继承了冲突的接口一样。如果不将引用转换为其他接口类型之一,组合接口的使用者通常将无法使用任一组件接口的成员,但所讨论的转换将被视为向上转换而不是向下转换。

在 .NET 中实现的方案在那里运行得很好。不幸的是,在 Java 中没有办法做类似的事情。我不知道如果一个接口继承了具有冲突成员的其他接口,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. 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. Unfortunately, there's no way to do anything similar in 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.

少年亿悲伤 2024-08-28 16:23:19

我认为没有定义名称,因为Java中的接口不能有方法实现,因此可以避免问题,因为特定方法总是只有一个实现,因此不会出现歧义。

我错过了重点还是你在谈论“c”变量?

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.

Have I missed the point or are you talking about the 'c' variable?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文