Influence of Java detailed parent class constructor sub-class constructor method

Detailed

1, if a class constructor is not defined, there is a default constructor no reference, the structure of the following format:

	public 类名(){
       super();
    }

2, this ([parameter list]) calls this constructor class , but must be valid on the first line of code line; and Super ([parameter list]) to call the parent class constructor , and also must be placed in the first valid code line; if it has a constructor calls this constructor method, the constructor does not directly call the parent class constructor. I.e., in a code segment, this ([parameter list]) and Super ([parameter list]) can only have one appears.

public class Father {//父类
	
	public Father() {
		super();
		System.out.println("父类构造方法");

	}
}
public class Son extends Father{//子类

	public Son(){
		this("Jim");
		System.out.println("子类构造方法");
	}
	public Son(String name) {
		super();
		System.out.println(name);
	}
}
public class Text {
	
	public static void main(String[] args) {
	
		   Son son = new Son();
	    
	}
}

3, if the method is not a subclass of this class constructor calls the constructor, not the parent class constructor call specified, the default call the parent class constructor with no arguments.
Note: The
parent class can not have two constructor with no arguments, it is understood, if nothing to write the system will default constructor parameters presented a no, but if in the class to write a parameter of the method, it is no longer Get a no-argument constructor.

to sum up

a, if the parent has a constructor (whether implicit or explicit), and the sub-class constructor with no arguments in does not clearly specify which constructor calls the parent class, subclass does not call the subclass other constructor method using super () constructor with no arguments to call the parent class implicit method

public class Father {//父类
	
	public Father() {
		super();
		System.out.println("父类构造方法");

	}
}
public class Son extends Father{//子类

	public Son(){
		System.out.println("子类构造方法");
	}
	public Son(String name) {
		this();
		System.out.println(name);
	}
}
public class Text {
	
	public static void main(String[] args) {
	    Son son = new Son("Jim");	    
	}
}

The output is:
parent class constructor
subclass constructor
Jim
in the Text class main method, call when Son (String name), this is called Son () function, and the constructor with no arguments subclass default call parent class the constructor with no arguments, so that the output from the above results.

B, if no parent class * * constructor with no arguments (whether implicit or explicit) is required to specify the subclass constructor must be invoked directly or indirectly the parent class constructor which a valid code on the first line and

public class Father {//父类
	String height;
	String name;
	public Father(String height,String name){
		this.name = "wjq";
		this.height = 165;
	}
	public Father(String name) {
		this.name = "wjq";
	}
}
public class Son extends Father{//子类
	String height = "165";
	String name = "wjq";
	public Son(String height,String name){
		super(name);
		System.out.println(name);
	}
	public Son(String name) {
		this(height,name)
		System.out.println(name);
	}
}
public class Text {
	
	public static void main(String[] args) {
	    Son son = new Son("Jim");	    
	}
}
Published 19 original articles · won praise 0 · Views 464

Guess you like

Origin blog.csdn.net/zzu957/article/details/104719444