- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有三门课
子二
public class ChildTwo {
public void methodOfChildTwo() {
System.out.println("methodOfChildTwo.");
}
}
ChildOne 扩展了 ChildTwo
public class ChildOne extends ChildTwo{
public void methodOfChildOne() {
System.out.println("methodOfChildOne.");
}
}
扩展 ChildOne 的父级
public class Parent extends ChildOne{
public void methodOfParent() {
System.out.println("methodOfParent.");
}
}
当我创建Parent对象时,我可以调用所有三个方法,因为所有这些方法都是在Parent类中继承的。
Parent parent =new Parent();
parent.methodOfParent();
parent.methodOfChildOne();
parent.methodOfChildTwo();
我的问题是
Parent extends ChildOne,ChildTwo
(Java 不支持这些语法)。尽管我们可以通过上述方式实现这一点,但为什么Java不支持这种语法?请有人澄清。
最佳答案
首先Java不支持多重继承,因为它可能会导致歧义。 (注意:像 C++ 这样的语言确实支持多重继承)。
1.Can I say this way I can achieve multiple inheritance in Java?
这仍然不是多重继承。
请参阅下面的图表:
您给定的代码就像右侧的图表。多重继承将直接从其所有父级继承所有属性和行为(Java 中不支持)。
2.This approach is conceptually similar with multiple extends, like Parent extends ChildOne,ChildTwo (Java does not supports these syntax).
您的方法实际上在概念上与多重扩展并不相似。 Java 不支持这一点以防止歧义。想象一下两个父类都有一个类似签名的方法。
class Bomb{
public void activate();
}
class LightBulb{
public void activate();
}
class Child extends Bomb, LightBulb{ //Imagine if this is allowed
//When I call activate(), will it activate the bomb or the lightBulb?
}
Why Java does not supports this syntax, although we can achieve this in above mentioned way?
两种情况不同,无法通过a extends b
、b extends c
实现多重继承。从概念上讲,它是不同的,因为层次结构完全不同。
在多重继承中,您想要扩展的两个父类可以完全不相关。想象一下Pegasus 扩展了 FlyingCreature、Horse
。
Pegasus
是一个FlyingCreature
,它也是也是一个马
,但是FlyingCreature
和 Horse
根本不相关。
在您给定的示例中,所有后续扩展父类都是另一个父类的子集。它们都是相关的。
哺乳动物
是 动物
和狮子
是 哺乳动物
和 is 也是 Animal。
如果您说您提到的方法在概念上类似于多重继承,请考虑以下场景:
您的任务是从 FlyingCreature 类
和 Horse 类
创建 Pegasus 类
。
你打算这样做吗?
class FlyingCreature{
}
class Horse extends FlyingCreature{ //But Horses do not fly!
}
class Pegasus extends Horse{ //You got what you want here, but the Horse class is wrong.
}
还是这个?
class Horse{
}
class FlyingCreature extends Horse{ //All flying creatures are horses? Are you sure?
}
//You got what you want here, but the FlyingCreature class is wrong.
class Pegasus extends FlyingCreature {
}
所以现在你看,这是不可能完成的,因为两个父类根本不相关。为了在某种程度上实现所谓的“多重继承”,Java 使用接口(interface)
。
关于java - 这是java中的多重继承吗?为什么Java不支持extends ChildOne,ChildTwo语法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31349072/
这段代码: interface I {} public class Test { TableView table; Test(ObservableList list) {
我们从 .NET 2.0 升级到 .NET 3.5。我的一位同事尝试在 Visual Studio 2008 中的调试器下运行 ASP .NET Web 项目时出现以下对话框。他可以正常构建,但无法调
我有一个具有class User extends Authenticatable的用户模型类,并且我也创建了另一个具有class Foo extends Model的模型类 这会在显示路线文件中的数据
我遇到的一个常见问题 @extend当试图用另一个 @extend 覆盖继承的属性时. 这是一个例子: // class selectors to be @extended // these coul
我对以下代码的 typescript 编译错误感到困惑: function f(x: T, y: S) { if (x === y) { // ERROR: This condition
这与对象 {} === {} 无关, found this issues不知道这个是不是一样 类型集 - AUnion 不是空集。另外两种类型(L 和R)正在扩展它。我的理解是这些 L、R 至少和 A
我收到以下错误: Extender Provider failed to return an Extender for this object 尝试为 .Net v4.7.2 加载 WCF 项目时。我
我收到以下错误: Extender Provider failed to return an Extender for this object 尝试为 .Net v4.7.2 加载 WCF 项目时。我
我刚刚在读Javascript: Module Pattern vs Constructor/Prototype pattern?我很好奇,当我们使用 $.fn.extend 或 $.extend 扩
我正在用 extend 做一些测试,在我做了一些观察后我有点困惑。初步观察: console.log($.extend === $.fn.extend); // trure // and since
我一直在使用一些通用方法从元素的可变参数创建集合,例如 public Set createSet( T... elements ) { ... 然而,最近我遇到了编译器没有按照我的预期去做的情况。以
刚去面试,问了一个问题。 面试官 - Java 是否支持多重继承? 我 - 不 面试官 - Java 中的每个类都扩展了类 Object(类 Object 除外),如果我们从外部扩展一个类,例如 Cl
我目前正在实现我的第一个 GWT 应用程序,我只是有一个快速的问题,关于在创建复杂的自定义小部件时 Extends Composite 和 Extend a specified widget 之间的区
使用 Observable 扩展 Object 和应用于以下类的扩展 Observable 之间有什么区别。 当应用程序运行时,结果是一样的。 library models; import 'pack
我制作了一个类装饰器,我想限制这个装饰器只能应用于某些类,所以我这样做了: @decorator() class A { foo:string; } @decorator() class B
在这个例子中: import java.util.*; public class Example { static void doesntCompile(Map> map) {} st
注意:这个问题与 Enum 无关,所以它不是重复的。Enum 被迫只与自身比较,因为编译器生成类型参数,而不是因为 java 递归类型参数。 我试图找到将类声明为的优势: public class S
我是 Java 的新手,正在尝试从 Java 泛型和集合一书中理解以下奇怪的语法。(我广泛使用 C++ 模板,因此可以声称了解泛型编程的基础知识和可能的陷阱): interface Collect
注意:这个问题与 Enum 无关,所以它不是重复的。Enum 被迫只与自身比较,因为编译器生成类型参数,而不是因为 java 递归类型参数。 我试图找到将类声明为的优势: public class S
有人知道是否可以延长 child Blade 吗? 我的应用程序有一个通用的布局模板,然后每个页面都从该模板@extends。每个页面都可以根据需要为其他 HTML block (例如模态)引入一系列
我是一名优秀的程序员,十分优秀!