gpt4 book ai didi

java - Java 中的多重继承。通过扩展命令分配优先级

转载 作者:行者123 更新时间:2023-11-29 02:58:17 25 4
gpt4 key购买 nike

<分区>

Oracle Java Tutorials, Multiple Inheritance of State, Implementation, and Type :

One reason why the Java programming language does not permit you to extend more than one class is to avoid the issues of multiple inheritance of state, which is the ability to inherit fields from multiple classes. For example, suppose that you are able to define a new class that extends multiple classes. When you create an object by instantiating that class, that object will inherit fields from all of the class's superclasses. What if methods or constructors from different superclasses instantiate the same field?

难道不能通过为子类扩展的所有类分配优先级来轻松解决这个问题吗?

class A extends B,C,D
{

}

让我们假设优先级是按照子类声明中它们被提及的升序分配的。

那么 B 的优先级 < C 的优先级 < D 的优先级。因此,如果这些类 B 、 C 或 D 中的任何一个有任何共同的状态或行为,那么优先级决定什么应该被继承,什么应该被隐藏。

请指教。提前致谢。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com