组成和继承相同吗?如果要实现合成模式,如何在Java中实现呢?
组成意味着HAS A 继承IS A
HAS A
IS A
Example:汽车有发动机,汽车是汽车
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. }