"Java Programming Ideas - Chapter 7 (Reuse Classes)"

Reuse class

Ways of reuse include composition and inheritance.
Composition: Creates an object of an existing class in a new class.
Inheritance: Take the form of an existing class and add new code.

7.1 Combination grammar

Just put the object reference in the new class. The location of object reference initialization can be:
1. In the place where the object is defined.
2. In the class constructor.
3. Just before these objects are used, this is called lazy initialization at the time.
4. Use instance initialization.

7.2 Inheritance syntax

When you create a class, you are always inheriting, if you don't explicitly say to inherit from another class, you implicitly inherit from the Object class.

1. Initialize the base class
When a derived class object is created, the object contains a sub-object of the base class. Java automatically inserts a call to the base class constructor in the constructor of the exported class.

Base class constructor: always called; don't call until the exported class is constructed.
Constructor with parameters: To call the constructor with parameters of the base class, use the super (parameter list) form.

/**
 * 调用基类默认构造器
 * @author Administrator
 *
 */
public class Cartoon extends Drawing{
    public Cartoon() {
        System.out.println("Cartoon Constructed");
    }
    public static void main(String[] args) {
        Cartoon cartoon = new Cartoon();
    }
}
class Art{
     Art() {
        System.out.println("Art Constructed");
    }
}
class Drawing extends Art{
    Drawing() {
        System.out.println("Drawing Constructed");
    }
}
/**
 * 调用基类带参构造器
 * @author Administrator
 *
 */
public class Chess extends BoardGame{

    Chess() {
        super(111);
        System.out.println("Chess Constructed");
    }
    public static void main(String[] args) {
        Chess chess = new Chess();
    }
}
class Game{
    Game(int i) {
        System.out.println("Game Constructed");
    }
}
class BoardGame extends Game{

    BoardGame(int i) {
        super(i);
        System.out.println("BoardGame Constructed");
    }

}

7.3 Proxy

Proxy is a compromise between composition and inheritance, using composition syntax to achieve inheritance-like functions. You can selectively expose the methods of the inherited class to the outside world.

/**
 * 代理--组合与继承的折中
 * @author Administrator
 *
 */
public class SpaceShipDelegation {
    private String name;
    private SpaceShipControls controls = 
            new SpaceShipControls();
    public SpaceShipDelegation(String name) {
        this.name = name;
    }

    public void up(int velocity){
        controls.up(velocity);
    }
    public void down(int velocity){
        controls.up(velocity);
    }   
    public static void main(String[] args) {
        SpaceShipDelegation shipDelegation = 
                new SpaceShipDelegation("MyShip");
        shipDelegation.up(100);
    }
}
class SpaceShipControls{
    void up(int velocity){
        System.out.println("up"+velocity);
    }
    void down(int velocity){}
}

7.5 Choosing before composition and inheritance

Both composition and inheritance allow placing child objects in a new class, composition is explicit, and inheritance is implicit.

Composition techniques are typically used in situations where a new class uses the functionality of an existing class rather than its interface.
Inheritance is taking an existing class and developing a special version of it.

"is-a" expresses inheritance relationship. "has-a" expresses a combinatorial relationship.

7.6 protected keyword

As mentioned earlier, the protected keyword is often used in inheritance.

As far as the class user is concerned, this is private, but it is indeed accessible to any exported class that inherits from this class or any other class that resides in the same package.
It's better to keep the domain private, and then control the access rights of the class's inheritors through the protected method class.

7.7 Upward Transformation

Inheritance not only provides methods for new classes, its most important aspect is to express the relationship between the new class and the base class.
The act of using a base class reference as a subclass reference is called upcasting .

write picture description here

Choice of composition and inheritance: If upcasting is necessary, inheritance is necessary; if not, consider whether composition can be used instead.

7.8 final keyword

There are three situations in which final may be used: data, methods and classes.
1. Final data
Constant data can include:

  • A compile-time constant that never changes.
  • A value that is initialized at runtime, and you don't want it to be changed.
    final modifies the basic data type, indicating that the value is constant.
    Final decorates the object reference, indicating that the reference point never changes (but the object itself can change).

A field that is both static and final occupies only a space that cannot be changed.

/**
 * public 可以用于包外
 * static 强调只有一份
 * final  则说它是一个常量
 */
public static final int value_Three = 39;

Blank final can be declared, but must be initialized when used.
final parameter: The parameter list can declare the parameter as final, which means that the object pointed to by the parameter reference cannot be changed in the method.
2.final
method There are several reasons for declaring a method as final:
* To lock the method. Want to make sure that the behavior of using methods in inheritance remains the same and won't be overridden.
* effectiveness.

All private methods in a class are implicitly final.
3.fina class
When a class is defined as final, it means that the class never needs to be changed and there will be no subclasses.
The methods of final classes are implicitly designated as final.

7.9 Initializing and loading classes

Loading in Java occurs when the first object of the class is created, and also when accessing a static field or static method.

First access the static method, load the base class (if there is an inheritance chain, load it up in turn), and then load the own class.
Object creation process: data initialization - calling base class constructor - initializing instance variables - calling this class constructor.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325405896&siteId=291194637