Home  >  Article  >  Java  >  Code examples of classes with inheritance relationships and their object initialization sequence in Java

Code examples of classes with inheritance relationships and their object initialization sequence in Java

不言
不言forward
2019-02-25 11:59:432690browse

This article brings you code examples about classes with inheritance relationships in Java and their object initialization sequence. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. .

Let’s talk about the conclusion first

For classes with inheritance relationships, their class and object construction sequence is: parent class’s class constructor () -> subclass’s class construction Device() -> Assignment of member variables of the parent class and instance code block -> Constructor of the parent class -> Assignment of member variables of the subclass and instance code block -> Constructor of the subclass.

The experimental code is as follows:

public class ExtensionTest {

    public static void main(String[] args) {
        new SubClass();
    }
}

class SuperClass
{
    {
        System.out.println("我是父类实例块");
    }
    static {
        System.out.println("我是父类类构造块");
    }
    public SuperClass()
    {
        System.out.println("我是父类构造函数块");
    }
}
class SubClass extends SuperClass
{
    {
        System.out.println("我是子类实例块");
    }
    static {
        System.out.println("我是子类类构造块");
    }
    public SubClass()
    {
        System.out.println("我是子类构造函数块");
    }

}

Result:

I am the parent class construction block
I am the subclass class construction block
I am Parent class instance block
I am the parent class constructor block
I am the subclass instance block
I am the subclass constructor block

Explanation:
The class construction block is the initialization class When executing, the initialization class must first load the class (of course it cannot be initialized without loading the class into the memory).
The class instance block is placed at the front of the class constructor and executed after the parent class constructor. Because before the constructor of the subclass is called, the constructor of the parent class will be called first.

Based on the above two rules, let’s look at the execution sequence.
new SubClass() means to construct an object of the SubClass class. To construct this object, you must first load the description and definition of this class into memory (class loading). Therefore, this class must be loaded first (but it has not been initialized yet).
After loading this class, you want to construct an object of this class. But at this time, the static variables of this class have not been initialized, so this class must be initialized first, but initializing this class requires initializing its parent class, so at this time it becomes, load parent class -> initialize parent class ( Call the static block, that is, the class construction block)
->Initialize the subclass (call the static block, that is, the class construction block).
Then you can construct the object of this class. Before constructing the object of this class, you must first construct the parent class object, so the constructor of the parent class will be called first, and before calling the parent class constructor, the parent class will be called first. instance block.
Then comes the subclass constructor. However, before execution, the instance block of the subclass must be called first, and finally the function body of the subclass constructor.







  • ##                                                                                                     

                                                                                                                                                                                                                                                                                                                                                                                      Let’s talk about the conclusion firstFor classes with inheritance relationships, their class and object construction order is: parent class’s class constructor () -> ; Class constructor () of the subclass -> Assignment of member variables of the parent class and instance code block -> Constructor of the parent class -> Assignment of member variables of the subclass and instance code block -> Construction of the subclass function. The experimental code is as follows:

    public class ExtensionTest {
    
        public static void main(String[] args) {
            new SubClass();
        }
    }
    
    class SuperClass
    {
        {
            System.out.println("我是父类实例块");
        }
        static {
            System.out.println("我是父类类构造块");
        }
        public SuperClass()
        {
            System.out.println("我是父类构造函数块");
        }
    }
    class SubClass extends SuperClass
    {
        {
            System.out.println("我是子类实例块");
        }
        static {
            System.out.println("我是子类类构造块");
        }
        public SubClass()
        {
            System.out.println("我是子类构造函数块");
        }
    
    }

    Result:

    I am the parent class construction block

    I am the subclass class construction block
    I am Parent class instance blockI am the parent class constructor blockI am the subclass instance block

    I am the subclass constructor block

    Explanation:

    The class construction block is the initialization class When executing, the initialization class must first load the class (of course it cannot be initialized without loading the class into the memory).

    The class instance block is placed at the front of the class constructor and executed after the parent class constructor. Because before the constructor of the subclass is called, the constructor of the parent class will be called first.


    Based on the above two rules, let’s look at the execution sequence.
    new SubClass() means to construct an object of the SubClass class. To construct this object, you must first load the description and definition of this class into memory (class loading). Therefore, this class must be loaded first (but it has not been initialized yet).
    After loading this class, you want to construct an object of this class. But at this time, the static variables of this class have not been initialized, so this class must be initialized first, but initializing this class requires initializing its parent class, so at this time it becomes, load parent class -> initialize parent class ( Call the static block, that is, the class construction block)
    ->Initialize the subclass (call the static block, that is, the class construction block).
    Then you can construct the object of this class. Before constructing the object of this class, you must first construct the parent class object, so the constructor of the parent class will be called first, and before calling the parent class constructor, the parent class will be called first. instance block.

    Then comes the subclass constructor. However, before execution, the instance block of the subclass must be called first, and finally the function body of the subclass constructor.








    • Code examples of classes with inheritance relationships and their object initialization sequence in Java#report

    You may be interested


    ##Comments

                                                                                               Sort by time



    Loading...

    Show more comments


The above is the detailed content of Code examples of classes with inheritance relationships and their object initialization sequence in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete