java constructor

1. If the constructor is customized, will the default constructor still be created and why?

  If the constructor is customized, the hidden no-argument constructor will no longer be provided.

    Question: Why does Java not provide a hidden constructor after customizing the constructor?

       The function of the constructor is to initialize member variables. The user-defined constructor is naturally like creating an object with a specific initial value. If an implicit constructor is also provided by default at this time,

        Then the external can create an object that does not meet the author's expectations.

        for example:

            

public class Rect {
    private float width;
    private float height;

    public Rect(float width, float height) {
        if (width <= 0 || height <= 0) {
            // Throws an exception if the parameter is unreasonable
            throw new IllegalArgumentException(
                    String.format("Size must > 0. w: %f, h: %f",
                            width, height));
        }
        this.width = width;
        this.height = height;
    }
}

 

 

 

2. Parent and child classes

    Question 1: If the parent class defines a constructor with parameters, and the subclass also defines a constructor with parameters, an error will be reported 

          Implicit super constructor P() is undefined. Must explicitly invoke another constructor

          It means that the parent constructor is not defined, and an other constructor must be specified.

          The reason is that when a subclass is created in java, a parent class needs to be created, and the subclass will call the hidden composition method of the parent class by default.

          But if you customize the parent constructor, the subclass cannot find the default constructor. So you need to explicitly specify which parent constructor to call.

          Two ways to solve: 

            1. Display and write out the default constructor of the parent class 

            2. In the subclass constructor method, it is clear which constructor of the parent class is called such as super(name);

          

package test;
plan 1 :
class P{
	static {System.out.println("P static");}
	{
		System.out.println("P con");
	}
	public P(){
		System.out.println("default p");
	}
	public P(String name){
		System.out.println("with parameter p");
	}
}
class C extends P{
	static {System.out.println("C static");}
	{
		System.out.println("C con");
	}
	public C(){
		
	}
	public C(String name){
		System.out.println("with parameter c");
	}
}
public class Test5 {
	public static void main(String[] args) {
		C c = new C("Zhang San");
	}
}

  

Scenario 2 :
package test;

class P{
	static {System.out.println("P static");}
	{
		System.out.println("P con");
	}
	
	public P(String name){
		System.out.println("with parameter p");
	}
}
class C extends P{
	static {System.out.println("C static");}
	{
		System.out.println("C con");
	}
	public C(){
		super("");
	}
	public C(String name){
		super(name);
		System.out.println("with parameter c");
	}
}
public class Test5 {
	public static void main(String[] args) {
		C c = new C("Zhang San");
	}
}

  

          

          

    3. Create subclass object calling sequence

      

package test;

class P{
    static {System.out.println("P static");}
    {
        System.out.println("P con");
    }
    
    public P(String name){
        System.out.println( "with parameter p" );
    }
}
class C extends P{
    static {System.out.println("C static");}
    {
        System.out.println("C con");
    }
    public C(){
        super("");
    }
    public C(String name){
        super(name);
        System.out.println( "with parameter c" );
    }
}
public class Test5 {
    public static void main(String[] args) {
        C c = new C("Zhang San" );
    }
}

result:  

  P static
  C static
  P con
  with parameter p
  C con
  with parameter c

    So the execution order is: parent static block - child static block - parent building block - parent constructor - child building block - child constructor 

    Note: Which question the parent constructor calls:  

        If not specified then the default constructor 

        If specified (such as super(name) in this example), the parameterized constructor of the parent class is called. .

 

 

 

 

 

 

 

    

Guess you like

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