简体   繁体   English

Java和接口中的多重继承

[英]Multiple Inheritance in Java and interfaces

我们可以说通过实现接口使Java中的多重继承成为可能吗?

java8接口的default方法中,我们将具有多个继承:)

No. You aren't really inheriting anything. 不,您实际上没有继承任何东西。 You are specifying behavior. 您正在指定行为。

It's not really "multiple-inheritance", you are simply outlining what the class should be able to do. 这并不是真正的“多重继承”,您只是概述了类应该能够做什么。

About as close a "multiple-inheritance" goes I suppose would be interfaces extending interfaces, really. 我想,“多重继承”实际上是接口扩展接口。

No. An interface defines how an implementation should communicate with the outside world you do not define any behavior. 否。接口定义了实现应如何与外界通信,而您没有定义任何行为。 You can of course implement multiple interfaces but that doesn't mean that you have multiple inheritance, just that the class implementing the interfaces can appear as different things. 您当然可以实现多个接口,但这并不意味着您具有多个继承,只是实现这些接口的类可以表现为不同的东西。

Nope. 不。 When you implement an interface, you are simply making a "promise" to implement certain methods in said class. 当实现接口时,您只是在“承诺”实现所述类中的某些方法。 When you extend another class, you inherit its methods and instance variables. 当扩展另一个类时,您将继承其方法和实例变量。 Two completely separate things. 两件事完全分开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM