Detailed notes on the construction method in Java

Constructor: The
    constructor is a special member of the class, it will be called when the class is instantiated. Divided into parametric structure and non-parameter structure. Each class has a no-parameter constructor by default. In addition to being called when the object is instantiated, the constructor can also assign values ​​to properties in the class.
Definition of construction method:

Three conditions must be met to define a construction method
1. The method name and the class name are the same.
2. There is no declaration of the return value type before the method.
3. You cannot use the return statement to return a value in a method.

Examples:

//定义一个Student类
public class Student {
    
    
    //定义一个无参构造方法
    public Student() {
    
    
        System.out.println("无参构造方法被调用了.......");
    }

    public static void main(String[] args) {
    
    
        //实例化一个对象
        Student student = new Student();
    }
}
运行结果:

Insert picture description here
It can be seen from the running results that the no-parameter construction method of this class is called when the student object is instantiated.
Examples (construction with parameters):

//定义一个Dog类
public class Dog {
    
    
    String dogName;
    String color;
    //定义一个有参构造方法,为成员属性赋值
    public Dog(String dogName, String color) {
    
    
        this.dogName = dogName;//为douName赋值
        this.color = color;//为color赋值
    }
    public void shouTime(){
    
    
        System.out.println(dogName + " " + color);
    }
    public static void main(String[] args) {
    
    
        //实例化Dog对象
        Dog dog = new Dog("金毛","黄色");
        dog.shouTime();
    }
}

Operation result:
Insert picture description here
overloading of the construction method: the
   same as the ordinary method, the construction method can also be overloaded. Multiple construction methods are defined in a class, as long as the parameter types and the number of parameters of the construction method are different. In this way, we can assign values ​​to different attributes through different construction methods.
Examples:

//定义一个Dog类
public class Dog {
    
    
    String dogName;
    String color;
    //定义一个参数的构造方法
    public Dog(String dogName) {
    
    
        this.dogName = dogName;
    }
    //定义两个参数的构造方法
    public Dog(String dogName, String color) {
    
    
        this.dogName = dogName;//为douName赋值
        this.color = color;//为color赋值
    }
    public void shouTime(){
    
    
        System.out.println(dogName + " " + color);
    }
    public static void main(String[] args) {
    
    
        //分别实例化d1和d2两个对象
        Dog d1 = new Dog("哈士奇", "黑白");
        d1.shouTime();
        Dog d2 = new Dog("哈士奇");
        d2.shouTime();
    }
}

Running result:
Insert picture description here
It can be seen from the running result that different parameters are called according to the different input parameters.

Come on! ! !

Guess you like

Origin blog.csdn.net/qq_42494654/article/details/109188396