JAVA class instantiation process

Java only supports single inheritance, not multiple inheritance, but supports the implementation of multiple interfaces at the same time
. Subclasses cannot inherit the constructor of the parent class, but:
in the constructor of the subclass, the constructor of the parent class must be called, and it must be placed in the The first line of the subclass constructor; when [this and super cannot be used at the same time]
is omitted, the no-argument constructor is called by default.

Java classes include: static code blocks, non-static code blocks (also called construction code blocks), static member variables, non-static member variables, constructors, member functions (no distinction between static/non-static)
1) No inheritance of parent classes In this case, the execution order when instantiating the object is:
static code block/static member>non-static code block/non-static member variable>constructor>member function
Among them: the parts at the same level are executed from front to back according to the position of occurrence
2) In the case of inheriting the parent class, the execution order when the child class instantiates the object:
the static code block/static member of the parent class > the static code block/static member of the child class > the non-static code block/non-static member variable of the parent class >Constructor of the parent class >Non-static code block/non-static member variable of the subclass >Constructor of the subclass
Where: The parts at the same level are executed from front to back according to the position of occurrence

Example:

class m_0019_s extends m_0019_f{
	static{
		System.out.println("子类静态代码块2");
	}
	static{
		System.out.println("子类静态代码块1");
	}
	
	{
		System.out.println("子类非静态代码块1");
	}
	
	public m_0019_s(){
		System.out.println("子类构造方法1");
	}

	{
		System.out.println("子类非静态代码块2");
	}
}

class m_0019_f {
	static{
		System.out.println("父类静态代码块2");
	}
	static{
		System.out.println("父类静态代码块1");
	}
	
	{
		System.out.println("父类非静态代码块1");
	}
	
	int i;
	static int j=2;
	
	public m_0019_f(){
		System.out.println("父类构造方法");
	}

	{
		System.out.println("父类非静态代码块2");
	}
}

public class m_0019{
	public static void main(String [] args) {
		m_0019_s m = new m_0019_s();
	}
}

operation result:

父类静态代码块2
父类静态代码块1
子类静态代码块2
子类静态代码块1
父类非静态代码块1
父类非静态代码块2
父类构造方法
子类非静态代码块1
子类非静态代码块2
子类构造方法1

Extension: Advanced subclass instantiation process

/**
 * 函数执行顺序测试
 */
public class m_0015 {
    public static void main(String [] args){
        System.out.println(new B().getValue());
    }
}

class A{
    protected int value;
    public A(int v) {
        setValue(v);
    }
    public void setValue(int value){
        this.value = value;//this指本类
    }
    public int getValue(){
        try{
            value++;
            return value;
        } catch(Exception e){
            System.out.println(e.toString());
        } finally {
            this.setValue(value);//this指当前对象
            System.out.println(value);
        }
        return value;
    }
}

class B extends A{
    public B() {
        super(5);
        setValue(getValue() - 3);
    }
    public void setValue(int value){
        super.setValue(2 * value);
    }
}
/**
 * 子类继承父类,调用方法时先是调用子类中的方法,如果没有就调用父类中的方法
 * try{ }、catch{ }、finally{ }返回值的问题,一旦try{ }中返回了某一个值,如果finally有返回值,finally中的返回值会覆盖try的返回值,如果finally没有返回值,就是try中的返回值。
 */

operation result:

22
34
17

 

Guess you like

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