some fragment of thinking in java part5

1. Delegation

a relationship that is not directly supported by Java. This is a midway between inheritance and composition, because you place a number object in the class you are building(like composition), but at the same time you expose all the methods from the member object in your new class(like inheritance)

public 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 dowm(int velocity) {

    controls.down(velocity);

  }

  public void forward(int velocity) {

    controls.forward(int velocitly);

  }

  ......

  public static void main(String[] args) {

    SpaceShipDelegation protector = new SpaceShipDelegation("NSEA Protector" );

    protector.forward(100);

  }

}

 

 

2.  Guaranteening proper cleanup

you must also pay attention to the calling order for the base-class and number object cleanup methods in case one subobject depends on another. In general, you should follow the same form that is imposed by a C++ complier on its destructors: reverse the order of creation.

3. Name hiding

if a Java base class has method name that is overloaded several times, redefineing that method name in the derived-class would not hide any of the base-class versions(unlike C++), Thus overloading works regardless of whether the method was defined at this level or in the base class

class Homer {

  char doh(char c) {

    print("doh(char)");

    return 'd';

  }

  float doh(float f) {

    print("doh(float)");

    return 1.0f;

  }

}

class Milhouse {}

class  Bart extends Homer {

  void doh(Milhouse m) {  // overloading between in inheritance ? I'm not sure

    print("doh(Milhouse)");

  }

}

public class Hide {

  public static void main(String[] args) {

    Bart b = new Bart();

    b.doh(1);

    b.doh('x');

    b.doh(1.0f);

    b.doh(new Milhouse());

  }

}/*Output:

doh(float)

doh(char)

doh(float)

doh(Milhouse)

*/

 

3. Chossing composition vs inheritance

Composition is  generally used when you want the functionality of an existing class inside your new class, but not its interface. That is, you embed an object so that you can it to implement features in your new class, but the user of your new class sees the interface you've defined for the new class rather than the interface from the embedded  object. For this effect, you embed private objects of existing classes inside your new class.

the is-a relationship is expressed with inheritance, and the has-a relationship is expressed with composition.

4. Protected

The protected keyword is a nod to pragmatism. available to anyone who inherits from this class or anyone else in the same package(protected also provides package access)

posted on 2015-10-26 17:12  terminator-LLH  阅读(175)  评论(0编辑  收藏  举报

导航