一尘不染

Java 继承与组成之间的区别

java

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


阅读 425

收藏
2020-02-28

共1个答案

一尘不染

组成意味着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.
}
2020-02-28