- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
做接口(interface)解决deadly diamond of death问题?
我不这么认为,例如:
// A class implementing two interfaces Interface1 and Interface2.
// Interface1 has int x=10 and Interface2 has int x = 20
public class MultipleInterface implements Interface1, Interface2{
public void getX(){
System.out.println(x);
}
}
这里我们得到一个不明确的 x
。
虽然接口(interface)是解决方法歧义的好方法,但我猜它们在变量的情况下会失败吗?
我说的对吗?如果我遗漏了什么,请启发我。
最佳答案
当一个类从父接口(interface)继承两个变量时,Java 坚持对相关变量名的任何使用都是完全限定的。这解决了问题。查看Java Language Specification Section 8.3 :
It is possible for a class to inherit more than one field with the same name. Such a situation does not in itself cause a compile-time error. However, any attempt within the body of the class to refer to any such field by its simple name will result in a compile-time error, because such a reference is ambiguous.
类似的声明适用于接口(interface) ( JLS §9.3 )。
the answer by Óscar López中的示例代码很棒。这是另一个例子:
class Base {
int x = 10;
}
interface Interface {
int x = 20;
}
class SingleInheritance implements Interface {
int y = 2 * x; // ok
}
class MultipleInheritance extends Base implements Interface {
int y = 2 * x; // compile-time error
int z = 2 * Interface.x; // ok
}
void aMethod(MultipleInheritance arg) {
System.out.println("arg.x = " + arg.x); // compile-time error
System.out.println("x = " + Interface.x); // ok
}
Java 8 为方法引入了一种有限形式的多重继承,因为接口(interface)现在可以声明 default methods子接口(interface)和实现类可以继承。由于一个类可以实现多个接口(interface),这可能会导致歧义,因为具有相同签名的不同默认方法可以从多个接口(interface)继承。1 Java 使用优先级方案来处理这个问题,以指定哪个默认方法实际上是遗传。当优先级方案未能产生单个获胜者时,它需要显式覆盖继承的默认方法。
请注意,在任何情况下,Java 都不会存在菱形问题,它是多重继承可能带来的问题的一个非常具体的子类。2“菱形”部分指的是类的形状解决问题所需的继承图。在 C++ 中,如果类 A 继承自两个类 B 和 C,每个类都继承自一个公共(public)基类 D,就会出现菱形继承(钻石问题)。在这种情况下,D 的任何公共(public)成员最终会在 A 中出现两次——一次是通过B 和一次通过 C。此外,无论何时构造或销毁 A 的实例,D 的构造函数或析构函数最终都会被调用两次(通常会带来灾难性的后果,因此名称中的“死亡”部分)。 C++ 通过提供虚拟继承 解决了这些问题。 (有关详细信息,请参阅讨论 here。)
1 请注意“不同”一词的使用。如果相同默认方法是通过两个父接口(interface)继承的,这两个父接口(interface)又扩展了定义默认方法的公共(public)基接口(interface),则没有问题;默认方法只是继承。
2 其他多重继承问题——例如 Java 中可能出现的接口(interface)字段、静态方法和默认方法的歧义——在技术上与菱形继承(钻石问题)无关(实际上, Deadly Diamond of Death 问题)。然而,关于该主题的大部分文献(以及该答案的早期版本)最终将所有多重继承问题归为“死亡钻石”这一标题。我想这个名字太酷了,只能在技术上合适的时候使用。
关于java - 接口(interface)是否解决了 "deadly diamond of death"问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9860811/
我想使用与此按钮相同的边框: (参见:https://graphicdesign.stackexchange.com/)。是否可以使用 radius XML 属性(borderLeftTopRadiu
我有四个带有列的表格: TABLE1: id TABLE2: id, t4_ref, t1_ref TABLE3: id, t4_ref, t1_ref TABLE4: id, type 我想要做的是
我正在尝试让我的 for 循环在给定用户特定最大值和最小值的情况下输出菱形,即使输入也是不允许的。任何想法,将不胜感激。练习并不像我想象的那么顺利。 #include #include using
我想谈谈有向无环图中从节点节点到另一个节点有多条路径的地方。这不是“循环”,我应该怎么称呼它?我正在使用术语“钻石”,但这意味着只有四个节点,这是不对的。 最佳答案 正如你所说,正确的术语不太可能是菱
我对一个Java项目有疑问: public class example { public Data getData() { List users = usersService.f
我对一个Java项目有疑问: public class example { public Data getData() { List users = usersService.f
我遇到了一个问题,我的代码在 NetBeans 中编译并运行良好,但是当我尝试使用 javac 通过命令行进行编译时,出现未经检查的警告错误,并且失败。当我使用 -Xlint:unchecked 进行
我有以下代码: class Base { public: virtual void doSomething() = 0; }; class BaseImpl : public
几年前,当 Java7 已经发布一年左右时,我从 C# 回到了 Java - java 泛型在某些方面对我来说似乎很奇怪。 有一个我会一遍又一遍地回来,看到这两个答案的受欢迎程度...... Diff
这个问题在这里已经有了答案: C++ virtual inheritance initializer list (2 个答案) 关闭 9 年前。 假设我们有一个菱形继承,其中 D 从 B1 和 B2
当我编译这个时: LinkedBlockingDeque q = new LinkedBlockingDeque<>(); 在 Eclipse Java EE Kepler 版本中,一切正常,但是一旦
Ruby 的版本是这样的: % ruby -v ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-darwin17] 我想到了如果我们在 Ruby 中以
这是讨论的后续问题: Why doesn't the diamond operator work within a addAll() call in Java 7? 来自 Java 教程, http:
我正在使用菱形方 block 算法生成随机地形。它工作正常,除了我得到这些大圆锥形状或者伸出或插入地形。问题似乎是时不时会有一个点设置得太高或太低。 这是问题的图片 当我将平滑度设置得非常高时可以更好
假设,我想创建一个如下所示的类并且不使用虚拟继承。从“下层”实例访问“上层”类成员的语法是什么?已经为左和右计算了一些方法,但不确定哪个是“规范”正确的。以及如何访问成员下---上层的方式? /*
我刚刚开始从事一个 Java 项目,并使用 IntelliJ 从 GitHub 下载了源代码——我以前从未使用过 IntelliJ,但听说这是一个比 Eclipse 好得多的 IDE(这就是我上次进行
考虑以下代码片段: class A { }; class B:public virtual A { }; class C:public virtual A { }; class D:public B,
假设我有一个应用程序有多个类(使用继承)现在有一天,我必须向我的应用程序添加新规范,这将需要我使用多重继承。 例如这段代码: class A{}; class B : public A{}; clas
我的项目暂时使用等距透视图,我在它们上方以网格格式显示坐标以进行调试。但是,当涉及到播放器的碰撞/网格锁定时,我遇到了一个问题。 由于 sprite 绘图的性质,我的数学计算在纹理的“三角形”角空白区
当一个类继承自同一基类派生的 2 个类时,我无法理解为什么虚拟继承在我们没有遇到类似于菱形继承(钻石问题)的问题的情况下有用。 当它仍然有用(或什至需要)时,有人能给我一个例子或解释吗? 谢谢:) 最
我是一名优秀的程序员,十分优秀!