7.复用类 的组合 、继承、代理三种使用方式

  类的复用 ,主要有 组合、继承、代理 三种方式。

7.1组合,就是 has-a的关系,而非 is-a.

package com.caolh._7ReusingClass;//: reusing/_7_1SprinklerSystem.java
// Composition for code reuse.

class WaterSource {
  private String s;
  WaterSource() {
    System.out.println("WaterSource()");
    s = "Constructed";
  }
  public String toString() { return s; }
}	
//喷水灭火系统
public class _7_1SprinklerSystem {
  private String valve1, valve2, valve3, valve4;
  private WaterSource source = new WaterSource(); // 我有一个水源 has -a
  private int i;
  private float f;
  public String toString() {
    return
      "valve1 = " + valve1 + " " +
      "valve2 = " + valve2 + " " +
      "valve3 = " + valve3 + " " +
      "valve4 = " + valve4 + "\n" +
      "i = " + i + " " + "f = " + f + " " +
      "source = " + source;
  }	
  public static void main(String[] args) {
    _7_1SprinklerSystem sprinklers = new _7_1SprinklerSystem();
    System.out.println(sprinklers);
  }
} /* Output:
WaterSource()
valve1 = null valve2 = null valve3 = null valve4 = null
i = 0 f = 0.0 source = Constructed
*///:~

7.2继承

package com.caolh._7ReusingClass;//: reusing/_7_2Chess.java
// Inheritance, constructors and arguments.


class Game {
  Game(int i) {
    System.out.println("Game constructor");
  }
}

class BoardGame extends Game {
  BoardGame(int i) {
    super(i);
    System.out.println("BoardGame constructor");
  }
}	

public class _7_2Chess extends BoardGame {
  _7_2Chess() {
    super(11); //父亲的构造器没有super()无参构造器,所以要自己指定
    System.out.println("Chess constructor");
  }
  public static void main(String[] args) {
    _7_2Chess x = new _7_2Chess();
  }
} /* Output:
Game constructor
BoardGame constructor
Chess constructor
*///:~

7.3代理的方式

package com.caolh._7ReusingClass;//: reusing/_7_3SpaceShipDelegation.java

 class SpaceShipControls {
  void up(int velocity) {}
  void down(int velocity) {}
  void left(int velocity) {}
  void right(int velocity) {}
  void forward(int velocity) {}
  void back(int velocity) {}
  void turboBoost() {}
} ///:~

public class _7_3SpaceShipDelegation {
  private String name;
  private SpaceShipControls controls =
    new SpaceShipControls();
  public _7_3SpaceShipDelegation(String name) {
    this.name = name;
  }
  // Delegated methods:
    //太空飞船代理了 飞船控制器
  public void back(int velocity) {
    controls.back(velocity);
  }
  public void down(int velocity) {
    controls.down(velocity);
  }
  public void forward(int velocity) {
    controls.forward(velocity);
  }
  public void left(int velocity) {
    controls.left(velocity);
  }
  public void right(int velocity) {
    controls.right(velocity);
  }
  public void turboBoost() {
    controls.turboBoost();
  }
  public void up(int velocity) {
    controls.up(velocity);
  }
  public static void main(String[] args) {
      _7_3SpaceShipDelegation protector =
      new _7_3SpaceShipDelegation("NSEA Protector");
    protector.forward(100);
  }
} ///:~

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值