继承和组合

继承和组合的区别

假设有三个类:Animal,Wolf和Bird,继承关系如下代码所示:

class Animal {
    private void beat() {
        System.out.println("心脏跳动...");
    }

    public void breath() {
        beat();
        System.out.println("吸一口气,吐一口气,呼吸中...");
    }
}

// 继承Animal,直接复用父类的breath()方法
class Bird extends Animal {
    public void fly() {
        System.out.println("我在天空自在的飞翔...");
    }
}

// 继承Animal,直接复用父类的breath()方法
class Wolf extends Animal {
    public void run() {
        System.out.println("我在陆地上的快速奔跑...");
    }
}

public class InheritTest {
    public static void main(String[] args) {
        Bird b = new Bird();
        b.breath();
        b.fly();
        Wolf w = new Wolf();
        w.breath();
        w.run();
    }
}

这是很清晰的继承关系,Wolf和Bird对象都可以直接复用Animal的方法,
将上面的三个类改成如下形式也可以实现相同的复用:

class Animal {
    private void beat() {
        System.out.println("心脏跳动...");
    }

    public void breath() {
        beat();
        System.out.println("吸一口气,吐一口气,呼吸中...");
    }
}

class Bird {
    // 将原来的父类组合到原来的子类,作为子类的一个组合成分
    private Animal a;

    public Bird(Animal a) {
        this.a = a;
    }

    // 重新定义一个自己的breath()方法
    public void breath() {
        // 直接复用Animal提供的breath()方法来实现Bird的breath()方法。
        a.breath();
    }

    public void fly() {
        System.out.println("我在天空自在的飞翔...");
    }
}

class Wolf {
    // 将原来的父类组合到原来的子类,作为子类的一个组合成分
    private Animal a;

    public Wolf(Animal a) {
        this.a = a;
    }

    // 重新定义一个自己的breath()方法
    public void breath() {
        // 直接复用Animal提供的breath()方法来实现Wolf的breath()方法。
        a.breath();
    }

    public void run() {
        System.out.println("我在陆地上的快速奔跑...");
    }
}

public class CompositeTest {
    public static void main(String[] args) {
        // 此时需要显式创建被组合的对象
        Animal a1 = new Animal();
        Bird b = new Bird(a1);
        b.breath();
        b.fly();
        // 此时需要显式创建被组合的对象
        Animal a2 = new Animal();
        Wolf w = new Wolf(a2);
        w.breath();
        w.run();
    }
}

此时,Wolf和Bird对象由Animal组合而成,运行程序可以得到与前面程序相同的执行结果。

  • 继承关系中从多个子类里抽象出共有父类的过程,类似于粗和关系中从多个整体类里提取被组合类的过程
  • 继承关系中从父类派生出子类的过程,类似于组合关系中把被组合类组合到整体类的过程。
  • 继承是对已有的类做一番改造,一次获得一个特殊的版本,也就是将一个较为抽象的类改造成能适用与某些特定需求的类。
  • 因此,对于上面的例子,使用继承更能表达其现实意义,用一个动物来合成一匹狼毫无意义:狼并不是由动物组成的。反之,如果两个类由明确的整体、部分的关系,例如Person需要复用Arm类的方法就应该采用组合关系来实现复用。

  • 继承表达的是(is a)的关系,而组合表达的是(has a)的关系

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值