Object oriented: construction methods

Object oriented (7): construction method

preface

Example 1:
class Student{              //Create a student class
    private String name;    //Define member variable name
    private int age;        //Define member variable age
public void setName(String name){ //Defines the setName method of the name public
        this.name = name;         //The parameter name is the same as the member variable name. Use this
    }
public String getName(){          //Defines the getName method of the name public
        return name;
    }
public void setAge(int age){      //Defines the setName method of the age public
    this.age = age;               //The parameter name is the same as the member variable name. Use this
}
public int getAge(){              //Define a public getName method
    return age;
}
public void show(){               //Define a show method to print all member variables
    System.out.println("full name:"+name+",Age:"+age);
}
}

//Test class
public class StructureDemo1 {
    public static void main(String[] args) {
        Student xx = new Student(); //Create student object values
        xx.show();                  //Call the show method to print all member variables
    }
}

            The execution results are:
                    full name: null,Age: 0

                    Process finished with exit code 0

Analysis: in example 1, we did not assign a value to the member variable, and finally the default values of null and 0 of the system will be output. If there is no assignment, why is the value of the system output? new Student() from new actually calls a parameterless constructor at the bottom

That is, the purpose of the construction method is to initialize the data in the object

Construction method format:
    (1)The method name is the same as the class name
    (2)No return value type, even void none Student(){}
    (3)No specific return value
Construction method format:
Example 2:
    class Student{              //Create a student class
        private String name;    //Define member variable name
        private int age;        //Define member variable age

        Student(){		//Create a construction method
        System.out.println("This is a parameterless construction method")
        }

    public void setName(String name){ //Defines the setName method of the name public
            this.name = name;         //The parameter name is the same as the member variable name. Use this
        }
		......(Middle (omitted)
public class StructureDemo1 {
    public static void main(String[] args) {
        Student xx = new Student(); //Create an object and run it directly
      }
   }
   The results are as follows:
           This is a parameterless construction method

           Process finished with exit code 0
           
 According to the execution results, new Come out Student()A parameterless constructor was called before printing"This is a parameterless construction method"

As can be seen from example 2, we proved that the parameter free construction method has been called internally; Recall that we didn't actually write construction methods for. Where did all the internal construction methods come from?

Example 3:
class Student02{

}
public class StructureDemo2 {
    public static void main(String[] args) {
        Student02 y = new Student02();
        System.out.println(y);
    }
}
    The execution results are:
    myself.day12.Student02@1540e19d

    Process finished with exit code 0

In example 3, we only created the object and then output it. The execution result is the address value. When we see the address value, it means that our object has been new; However, in the program, we did not write a parameterless construction method,

Therefore, we can know that the Java virtual machine JVM will automatically provide us with a parameter free construction method

Note: if we create our own constructor, the JVM will no longer provide the default parameterless constructor. As we learned earlier, methods can be overloaded in the same class (the method name is the same, the parameter list is different, and it has nothing to do with the return value)

We create a method by overloading the method (the name is the same as that of the nonparametric method, but the parameter list is different). At this time, this method is called the constructor method
 Example 4:
class Student02{
    private String name;
    private int age;
    Student02(){
        System.out.println("This is the parameterless construction method we added ourselves");
    }

    Student02(String name){//Through method overloading (the method names are the same, and the parameter lists are different)
        this.name = name;  //We created a parameter constructor with a name parameter
    }

    Student02(int age){ //Through method overloading, we create a parameter construction method with age parameter
        this.age = age;
    }

    public void show(){
        System.out.println("full name:"+name+",Age:"+age);
    }

}
public class StructureDemo2 {
    public static void main(String[] args) {
        Student02 y = new Student02();
        System.out.println(y);

        //Create the second object, because it is a method with parameters, we can enter parameters
        Student02 n =new Student02("Li Yuwei");
        n.show();//Li Yuwei, 0

        //Create the third object, because it is a method with parameters, we can enter parameters
        Student02 a =new Student02(17);
        a.show();//null,17
    }
}
        The execution results are:
                This is the parameterless construction method we added ourselves
                myself.day12.Student02@1540e19d
                Name: Li Yuwei,Age: 0
                full name: null,Age: 17

                Process finished with exit code 0

It can be seen from example 4 that the method is overloaded. We create a construction method with parameters for name and age separately, and create an object separately to call the show() method for output; However, the output results cannot be on the same line; Next, we use the parameter constructor to write the name and age parameters in the same method.

Example 5:
class Student02{			//Create a class
    private String name;	//Define member variable name
    private int age;		//Define member variable age
    
    Student02(){			//Create a parameterless constructor
    }

    Student02(String name){	//Create a parameter constructor with a name parameter
        this.name = name;
    }

    Student02(int age){		//Create a parameterized construction method with an age parameter
        this.age = age;
    }

   Student02(String name,int age){	//Create a parameter constructor with name and age parameters
        this.name = name;		//It is also called parameter construction method with all member variable parameters
        this.age = age;
   }
    public void show(){		//Create a show method, and call this method to output all member variables
        System.out.println("full name:"+name+",Age:"+age);
    }
}

public class StructureDemo2 {	//Create a test class
    public static void main(String[] args) {
    	//Create an object that can be assigned in parentheses
        Student02 na = new Student02("Li Yuwei",18);
        na.show();	//Call the show method
    }
}
        The execution results are:
                Name: Li Yuwei,Age: 18

                Process finished with exit code 0

Function of construction method:

You can assign values to the object's member variables when creating the object (parameters can be written in parentheses, representing assignment)

be careful:

When we create our own constructor, whether it is parameterless or with parameters, the JVM will never provide parameterless constructor again

If we delete the parameterless construction method and only retain the parameterless construction method, the system will report an error

Tags: Java

Posted on Sun, 05 Dec 2021 14:56:14 -0500 by jjoske