Re reading the combination and inheritance of the series of Java programming ideas

Java reuses code in two ways: combination and inheritance.

combination

Composition simply places the object reference in a new class. For example, we have a class B, which has a say method. We use the method of class B in class A, which is composition.

public class B {

    public void say(){

    }
}
public class A {

    public void combo(){
        B b = new B();
        b.say();
    }
}

In the Java programming idea, four ways of initializing references are also introduced. 1. Where the object is defined.

public class Bath{
    private String s1 = "happy";
    public Bath(){};
}

2. In the constructor

public class Bath{
    private String s1;
    public Bath(){
        s1="happy";
    };
}

3. When using objects, initialization is also called lazy initialization. This way can reduce the additional burden.

class Lazy{
}
public class Bath{
    private Lazy lazy;
    public Bath(){
      
    };
    public void initLazy(){
        lazy = new Lazy();
    }
}

In this way, the lazy object will be initialized only when bath's initlazy method is called. If it is not called, it will not be initialized. 4. Use instance initialization. This is the simplest. The instance initialization we often use is like this.

public class Bath{
    public static void main(String args){
        Bath bath = new Bath();
    }
}

inherit

When we create a class, we inherit all the time. If we don't specify which class to inherit, we implicitly inherit the object class. Inherit the extends keyword used. The subclass will inherit all member variables and methods of the parent class. A major principle of inheritance is that we set all data members to private and methods to public. This makes it easy for other classes to access all the methods of the inherited class. Java uses the super keyword to call the constructor of the parent class. When the subclass inherits the parent class, the constructor of the parent class will be called automatically. If all constructors are parameterless constructors, the call does not need to be displayed. If it is a parameterless constructor, the constructor that needs to be displayed when the subclass inherits will be called with super.

class Art {
    Art(){
        System.out.println("art");
    };
}

class Drawing extends Art{
    Drawing(){
        System.out.println("Drawing");
    }
}

public class Cartoon extends Drawing{
    Cartoon(){
        System.out.println("cartoon");
    }

    public static void main(String[] args) {
        Cartoon cartoon = new Cartoon();
    }
}

The results are as follows:

class Art {
    Art(String s){
        System.out.println("art");
    };
}

class Drawing extends Art{
    Drawing(){
        super("a");
        System.out.println("Drawing");
    }
}

There is an exercise in the book to prove that the base class constructor, a, is always called, and B, is called before exporting the class constructor. The above example proves two points at the same time. The code of a subclass constructor is always executed last. The print code in the parent constructor is then printed on the console.

agent

The book also introduces the concept of agent, which is also a form of reuse. So how do you understand this. The examples in the book are very good. For example, we have a spacecraft control module. Here we only write a code that wants to fly up and down.

public class SpaceShipControls{
    void up(int a){};
    void down(int a){};
}

We will build another spaceship. The spaceship can certainly fly up and down. Therefore, our first scheme adopts inheritance. Directly call the code of the controller.

public class SpaceShip extends SpaceShipControls{
    public static void main(String[] args){
        SpaceShipControls s = new SpaceShipControls();
        s.up(100);
    }
}

At present, we have all the methods of the controller in our spacecraft. In a sense, spaceships become space controllers. This should not be the case. The spacecraft should send instructions to the space controller. Let's use the proxy method to create this code. Let's take another look.

public class SpaceShipDelegation{
    private SpaceShipControls s = new SpaceShipControls();
    
    public void up(int a){
        s.up(a);
    }

    public void down(int a){
        s.down(a);
    }

    public static void main(String args){
        SpaceShipDelegation sa = new SpaceShipDelegation();
        sa.up(100);
    }
}

Through this proxy, we can have more control and choose to provide a subset of the re member object methods.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>