Let's first look at what a constructor is:
1. Constructors are also called construction methods or constructors, which are divided into parametric constructors and nonparametric constructors;
2. Constructor is also a method, just a special method, it will be called when the object is created;
3. The most important function of the constructor is to initialize the object when creating the object. The parameter constructor can realize the object parameter passing (see the convenience of the parameter constructor later);
4. A class can have zero constructors (if you don't have a compiler defined by yourself, it will help you to provide a parameterless constructor) or multiple constructors (a small partner who doesn't know the overload definition can write down the concept first);
5. Constructors cannot be inherited by subclasses. Subclasses in Java will automatically call the constructors of the parent class (also, you can write down the concepts or skip the ones you don't know)
Since the constructor is a special method, let's look at the difference between the construction method and the common method:
1. Naming: the method name of the constructor must be the same as the class name. In general, the method name of a method can be defined freely under the naming specification.
2. Modifiers: constructors cannot be decorated with static, final, synchronized, abstract, and native
3. Return value: the constructor does not have a return value (but does not need to write void). In general, the method must have a return value or no return value (void)
Let's take a look at the code of the nonparametric constructor and see how to assign values to properties in the case of nonparametric constructor (without defining the same principle of constructor):
1 package test; 2 3 public class Student01 { 4 / / define properties 5 public String name; 6 public int age; 7 public String sex; 8 9 /** 10 * construction method without parameters: 11 * if the compiler is not written, it will be added automatically; as long as the constructor is defined, the compiler will no longer help you define whether it has parameters or not 12 */ 13 when public student01() {/ / new object is called, it will give an initialization value to the new object (that is, the number is 0, the character is null, the Boolean is false, and all references are null) 14 System.out.println ("calling me in the process of creating objects, executing at s1.name =" Zhang three "), so I print before the print method of main method". 15 } 16 17 public static void main(String[] args) { 18 Student01 S1 = new Student01(); / / Student01() will be called automatically during this step 19 / / assign a value to a property through the object. Property 20 s1.name = "Zhang San"; 21 s1.age = 18; 22 s1.sex = "male"; 23 system. Out. Println ("Name:" + s1.name+"\n gender:" + s1.sex+"\n age:" + s1.age); 24 } 25 26 }
Results:
I was called in the process of creating objects, before s1.name = "Zhang three", before execution, so I printed before the printing method of main method.
Name: Zhang San
Gender: Male
Age: 18
Let's look at the parameter constructor (assign a property without passing another object. Property)
1 package test;
2
3 public class Student02 {
4 // Defining attributes
5 public String name;
6 public int age;
7 public String sex;
8
9 //Defining a parametric construction method
10 public Student02(String name, int age, String sex) { //Similar to ordinary method with parameters public void add(int a,int b){}
11 this.name = name; //Assign the value of the parameter variable to the member variable. this refers to the current object. You don't need to panic with your friends. They mean:
12 this.age = age; //Take name for examplethis.name = name;That's what's coming down here name Value (Zhang Cuihua) is assigned to the(public String name;)name
13 this.sex = sex;
14 }
15
16 public static void main(String[] args) {
17 Student02 s2 = new Student02("Zhang Cu Hua", 18, "female");//When calling the constructor, pass parameters (change the default initialization), similar to calling ordinary methods with parameters add(1,1);
18 System.out.println("Full name:"+s2.name+"\n Gender:"+s2.sex+"\n Age:"+s2.age);
19 }
20
21 }
Constructors can not only define one, but can be zero (when an object was created without a constructor) or multiple
1 package test; 2 3 public class Student02 { 4 // Defining attributes 5 public String name; 6 public int age; 7 public String sex; 8 9 //Defining a parametric construction method 10 public Student02(String name, int age, String sex) { //Similar to ordinary method with parameters public void add(int a,int b){} 11 this.name = name; 12 this.age = age; 13 this.sex = sex; 14 } 15 16 //Define nonparametric construction method 17 public Student02() { 18 19 } 20 21 //The construction method (overload) of defining a two parameter cannot be the same as that just mentioned. Note: the name should still be consistent with the class name 22 public Student02(String name, String sex){ 23 this.name = name; 24 this.sex = sex; 25 } 26 27 public static void main(String[] args) { 28 Student02 s2 = new Student02("Zhang Cu Hua", 18, "female");//When calling the constructor, passing parameters is similar to calling ordinary methods with parameters add(1,1); 29 System.out.println("Full name:"+s2.name+"\n Gender:"+s2.sex+"\n Age:"+s2.age); 30 31 System.out.println("---------------------"); 32 33 //Create a new object 34 Student02 s3 = new Student02("Pomegranate elder sister", "female");//Here, you can use any of the three previous constructors 35 System.out.println("Full name:"+s3.name+"\n Gender:"+s3.sex+"\n Age:"+s3.age); 36 //Because a constructor is used here that does not pass a value to age, the printing age is the initial value of 0 37 } 38 39 }
Result:
Name: Zhang Cuihua
Gender: Female
Age: 18
---------------------
Name: sister pomegranate
Gender: Female
Age: 0