Java object oriented constructor

Java object oriented constructor

Use the constructor to ensure initialization

Function: perform initialization when creating objects.

In Java, each class has at least one constructor. The format is as follows:

[修饰符] 构造器名(参数列表){
  ...执行体
  }

At this time, I was suddenly confused. There was no constructor in the code I had written before. What happened?

Look at the following statement first:

Student s0 = new Student();

This is what we often wrote before. We call it the process of creating an object and letting the reference variable point to the object (class instantiation). When executing new student(); Statement, the system will allocate memory space for the object and call the corresponding constructor to execute the execution body of the response, so as to ensure that the object has been properly initialized before being operated by us.

However, there is no constructor in our previous code. This is because when we design a class, if no constructor is defined, the system provides a default constructor (i.e. no parameter constructor) for the class by default, and the execution body is empty.

be careful:

About constructor access modifiers:

other:

Constructor Overload

If we want to create objects in multiple ways, we need to design multiple constructors, but the constructor name must be the same as the class name, so it will naturally lead to the overload of the constructor.

Constructor overload: if there are multiple constructors in the same class and the constructor parameter list is different, it is a constructor overload.

Constructor overload code demonstration:

package com.my.pac09;

/**
* @author Summerday
* @date 2019/12/6 18:23
*/
public class Student {
   //暂且先把属性设为public
   public String name;
   public int grade;

   //重新定义默认构造器,假如输出语句
   public Student() {
       System.out.println("创建了一个学生");
   }
   //this的用法之一
   public Student(String name) {
       this.name = name;
       System.out.println("创建了一个名为" + this.name + "的学生");

   }

   public Student(String name,int grade) {
   //调用另一个构造器
       this(name);
       this.grade = grade;
       System.out.println("创建了一个名为" + this.name + "的学生," + this.grade + "年级");
   }
}

Overload constructor call: when the system calls the constructor through new, it determines which constructor to use according to the passed in argument list.

package com.my.pac09;

/**
 * @author Summerday
 * @date 2019/12/6 18:29
 */
public class StudentTest {
    public static void main(String[] args) {
        //用new创建对象,调用新定义的无参构造器
        Student s0 = new Student();
        //两个属性值默认初始化
        System.out.println(s0.name+","+s0.grade);//null

        //调用重载构造器,为name赋值
        Student s1 = new Student("小明");
        System.out.println(s1.name+","+s1.grade);//小明

        Student s2 = new Student("小红",2);
        System.out.println(s2.name+","+s2.grade);//小红

    }
}

//输出
创建了一个学生
null,0
创建了一个名为小明的学生
小明,0
创建了一个名为小红的学生
创建了一个名为小红的学生,2年级
小红,2

About the keyword this (some usage will be summarized in the next article):

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>