复用类

来源:互联网 发布:gta5最好用的漂移数据 编辑:程序博客网 时间:2024/06/15 21:46

一、组合

这里写图片描述

package reusing;// Constructor initialization with composition.import static net.mindview.util.Print.*;class Soap {  private String s;  Soap() {    print("Soap()");    s = "Constructed";  }  public String toString() { return s; }}   public class Bath {  private String // Initializing at point of definition:    s1 = "Happy",    s2 = "Happy",    s3, s4;  private Soap castille;  private int i;  private float toy;  public Bath() {    print("Inside Bath()");    s3 = "Joy";    toy = 3.14f;    castille = new Soap();  }   // Instance initialization:  { i = 47; }  public String toString() {    if(s4 == null) // Delayed initialization:      s4 = "Joy";    return      "s1 = " + s1 + "\n" +      "s2 = " + s2 + "\n" +      "s3 = " + s3 + "\n" +      "s4 = " + s4 + "\n" +      "i = " + i + "\n" +      "toy = " + toy + "\n" +      "castille = " + castille;  }   public static void main(String[] args) {    Bath b = new Bath();    print(b);  }} /* Output:Inside Bath()Soap()s1 = Happys2 = Happys3 = Joys4 = Joyi = 47toy = 3.14castille = Constructed*///:~

二、继承

注意要对父类对象进行初始化。

//: reusing/Detergent.java// Inheritance syntax & properties.import static net.mindview.util.Print.*;class Cleanser {  private String s = "Cleanser";  public void append(String a) { s += a; }  public void dilute() { append(" dilute()"); }  public void apply() { append(" apply()"); }  public void scrub() { append(" scrub()"); }  public String toString() { return s; }  public static void main(String[] args) {    Cleanser x = new Cleanser();    x.dilute(); x.apply(); x.scrub();    print(x);  }}   public class Detergent extends Cleanser {  // Change a method:  public void scrub() {    append(" Detergent.scrub()");    super.scrub(); // Call base-class version  }  // Add methods to the interface:  public void foam() { append(" foam()"); }  // Test the new class:  public static void main(String[] args) {    Detergent x = new Detergent();    x.dilute();    x.apply();    x.scrub();    x.foam();    print(x);    print("Testing base class:");    Cleanser.main(args);  } } /* Output:Cleanser dilute() apply() Detergent.scrub() scrub() foam()Testing base class:Cleanser dilute() apply() scrub()*///:~

三、代理

是继承和组合之间的中庸之道,将一个成员对象置于要构造的类中(组合),但与此同时在新的类中暴露了该对象的所有方法(类似继承)。

//: reusing/SpaceShipDelegation.javapublic class SpaceShipDelegation {  private String name;  private SpaceShipControls controls =    new SpaceShipControls();  public SpaceShipDelegation(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) {    SpaceShipDelegation protector =      new SpaceShipDelegation("NSEA Protector");    protector.forward(100);  }} ///:~

四、结合使用继承和组合

确保正确清理

除了内存以外,不要信任垃圾回收器和finalize()方法。

//: reusing/CADSystem.java// Ensuring proper cleanup.package reusing;import static net.mindview.util.Print.*;class Shape {  Shape(int i) { print("Shape constructor"); }  void dispose() { print("Shape dispose"); }}class Circle extends Shape {  Circle(int i) {    super(i);    print("Drawing Circle");  }  void dispose() {    print("Erasing Circle");    super.dispose();  }}class Triangle extends Shape {  Triangle(int i) {    super(i);    print("Drawing Triangle");  }  void dispose() {    print("Erasing Triangle");    super.dispose();  }}class Line extends Shape {  private int start, end;  Line(int start, int end) {    super(start);    this.start = start;    this.end = end;    print("Drawing Line: " + start + ", " + end);  }  void dispose() {    print("Erasing Line: " + start + ", " + end);    super.dispose();  }}public class CADSystem extends Shape {  private Circle c;  private Triangle t;  private Line[] lines = new Line[3];  public CADSystem(int i) {    super(i + 1);    for(int j = 0; j < lines.length; j++)      lines[j] = new Line(j, j*j);    c = new Circle(1);    t = new Triangle(1);    print("Combined constructor");  }  public void dispose() {    print("CADSystem.dispose()");    // The order of cleanup is the reverse    // of the order of initialization:    t.dispose();    c.dispose();    for(int i = lines.length - 1; i >= 0; i--)      lines[i].dispose();    super.dispose();  }  public static void main(String[] args) {    CADSystem x = new CADSystem(47);    try {      // Code and exception handling...    } finally {      x.dispose();    }  }} /* Output:Shape constructorShape constructorDrawing Line: 0, 0Shape constructorDrawing Line: 1, 1Shape constructorDrawing Line: 2, 4Shape constructorDrawing CircleShape constructorDrawing TriangleCombined constructorCADSystem.dispose()Erasing TriangleShape disposeErasing CircleShape disposeErasing Line: 2, 4Shape disposeErasing Line: 1, 1Shape disposeErasing Line: 0, 0Shape disposeShape dispose*///:~

五、向上转型

//: reusing/Wind.java// Inheritance & upcasting.class Instrument {  public void play() {}  static void tune(Instrument i) {    // ...    i.play();  }}// Wind objects are instruments// because they have the same interface:public class Wind extends Instrument {  public static void main(String[] args) {    Wind flute = new Wind();    Instrument.tune(flute); // Upcasting  }} ///:~

组合和继承的选择:是否用到向上转型?
组合一般是将现有类型作为新类型底层实现的一部分加以复用,而继承复用的是接口。


六、初始化及类的加载

//: reusing/Beetle.java// The full process of initialization.import static net.mindview.util.Print.*;class Insect {  private int i = 9;  protected int j;  Insect() {    print("i = " + i + ", j = " + j);    j = 39;  }  private static int x1 =    printInit("static Insect.x1 initialized");  static int printInit(String s) {    print(s);    return 47;  }}public class Beetle extends Insect {  private int k = printInit("Beetle.k initialized");  public Beetle() {    print("k = " + k);    print("j = " + j);  }  private static int x2 =    printInit("static Beetle.x2 initialized");  public static void main(String[] args) {    print("Beetle constructor");    Beetle b = new Beetle();  }} /* Output:static Insect.x1 initializedstatic Beetle.x2 initializedBeetle constructori = 9, j = 0Beetle.k initializedk = 47j = 39*///:~
原创粉丝点击