Java 继承与组成之间的区别

组成和继承相同吗?如果要实现合成模式,如何在Java中实现呢?

回答:

组成意味着HAS A

继承IS A

Example:汽车有发动机,汽车是汽车

在编程中,它表示为:

class Engine {} // The Engine class.

class Automobile {} // Automobile class which is parent to Car class.

class Car extends Automobile { // Car is an Automobile, so Car class extends Automobile class.

private Engine engine; // Car has an Engine so, Car class has an instance of Engine class as its member.

}

以上是 Java 继承与组成之间的区别 的全部内容, 来源链接: utcz.com/qa/398438.html

回到顶部