Java是否支持多重继承? [英] Does Java support Mulitiple Inheritance?

查看:504
本文介绍了Java是否支持多重继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Java事实来看,Java中的所有类都有一个父类,如Object.但是同一Java表示它不支持多重继承.但是这段代码是什么意思?

From the Java fact that all class in Java have a parent class as Object. But the same Java says that it doesn't support multiple inheritance. But what this code mean ?

public class A extends B
{
          //Blah blah blah
}

根据上面的代码,这意味着Class A扩展了Class B.现在,类A也继承了Object类的属性,该属性是B的超类.这不是说Class A继承了Class BObject Class,这就是我们所说的多重继承权吗?

From the above code it means that Class A extends Class B. Now Class A also have inherited the properties of Object class which is the super class of B. Is this doesn't mean that Class A have inherited both Class B and Object Class, this what we called Multiple inheritance right?

因此,现在Java支持多重继承,如果不支持,那么上述代码(显示多重继承)的答案是什么

So now Java supports Multiple inheritance, if not then what is the answer for the above code(which shows multiple inheritance)

推荐答案

不,这是单一继承. A从B继承,B从Object继承.

No, this is single inheritance. A inherits from B, B inherits from Object.

多重继承是ABC扩展而来的,其中BC彼此不继承,这可能导致

Multiple inheritance would be A extends from B and C, where B and C don't inherit from each other, which can cause the Diamond Problem:

如果B定义了方法foo(),并且C也定义了方法foo(),我会这样做:

If B defines a method foo() and C also defines a method foo(), and I do this:

new A().foo();

将使用哪个foo()实现?

在许多语言中这都是一个巨大的问题,因此Java设计人员决定不允许多重继承.

This is a huge problem in many languages, so the Java designers decided not to allow multiple inheritance.

这篇关于Java是否支持多重继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆