Static code blocks and inheritance

/*Characteristics of the construction method
    in the subclass 1. When the subclass constructs the object, it is found that the parent class is also running when the subclass construction method is accessed. Why?
          Answer: Because, in the first line of the subclass construction method There is a default implicit statement: super();
          the instantiation process of the subclass: all the constructors in the subclass will access the empty parameter constructors in the parent class by default.

    2. Why do you need to access the construction method in the parent class when the subclass is instantiated?
         Answer: Because, when the subclass inherits the parent class, the content in the parent class is obtained, so before using the content of the parent class, you must first See how the parent class initializes its own content; so when the subclass constructs the object, it must access the construction method in the parent class. In order to complete this necessary action, super() is added to the subclass construction method; Statement. ps: If a structure with parameters is defined in the parent class, but no parameterless structure is defined, then super (parameter list) must be used in the subclass construction method to specify the construction method in the parent class to be called.
     Note: super The statement must be placed in the first line of the subclass construction method, because the initialization action of the parent class must be completed first. The
    
initialization process when creating the subclass object:
       first open up memory space in the heap memory -> parent class, subclass Default initialization----->Subclass structure pushes the stack----->Execute the first line super()----->Parent class structure pushes the stack
    ---->Parent class display initialization--- -->Parent class construction code block initialization----->Parent class construction method initialization----->Parent class construction method execution finished----->Back to subclass construction
    ----->Child Class explicit initialization----->Subclass construction code block initialization----->Subclass construction method initialization----->Subclass construction method execution completed---->Initialization completed.

Another point: whether it is in the method of the subclass or the parent class, if there is this by default, then this this points to the subclass object, that is, in the method of the parent class, if there is this (you can also say It is not modified by static), all point to the memory area where the object of the subclass is located, not the parent class, because there is no parent class object at this time.
--------------- ------
author: hurricane_ning
source: CSDN
original: https: //blog.csdn.net/weixin_36898943/article/details/79762489
copyright: This article is a blogger original article, reproduced, please attach Bowen link! */
/*The static code block will be executed as the class is loaded, and only once.
 * When new XX() starts to execute, it will first execute the static code block in the parent class,
 * then execute the static code block in the subclass,
 * When all the static code blocks are executed, it will execute
 * main function The output statement in (provided that the output statement is before new StaticTest()),
 * Then the non-static code block in the parent class will be executed, followed by the constructor in the parent class
 *, and then the non-static code in the subclass will be executed The code block, and finally the constructor in the subclass, that's it!
---------------------
Author: a self-taught programmer
Source: CSDN
Original: https: //blog.csdn.net/sinat_33921105/article/details/79509638
Copyright statement: This article is the original article of the blogger, please attach the link to the blog post if you reprint it! */

/*Static code block: It is executed as the class is loaded, and it is executed only once, and takes precedence over the main function. Used to initialize the class. */
//The parent class constructor is
called before the subclass constructor./*Before the subclass constructor is called, the parent class constructor will be called first. When the subclass constructor is not used "super (parameter or no parameter)" When you specify to call the parent class constructor, the parent class’s parameterless constructor is called by default. If the parent class contains a parameter constructor but no parameterless constructor, you must use "super(parameter )" specifies the parameterized constructor of the parent class to be called, otherwise an error will be reported.
Static block: declared with static, executed when the JVM loads the class, and executed only once.
Building block: defined directly in the class with {}, executed every time an object is created.
Priority of execution order: static block>main()>building block>construction method
static block is executed in accordance with the stated order
---------------------
author: soliderzzz
source: CSDN
original: https: //blog.csdn.net/qq_18975791/article/details /80728041
Copyright statement: This article is the original article of the blogger, please attach the link to the blog post if you reprint it! */
 

public class A {
    static {System.out.println("A的static");
    }
    public A() {
        System.out.println("A的构造方法");
    }

}
public class B extends A {
    static {
        System.out.println("B的static");
    }
    public B() {
        System.out.println("B的构造方法");
    }

}
public class Test {
public static void main(String args[]) {
   A ab=new B();
   ab=new B();
}
}

 

Guess you like

Origin blog.csdn.net/qq_36073688/article/details/88119339