day05 object-oriented first day learning (summary)

2、basic

3. This pointer

4、construction

The role of the construction method

The construction method is the last step in creating an object,Is to help us assign values ​​to member variables. This method is not called by ourselves, butIs automatically called by jvm. [It’s good for the core to understand this]

public class Demo {
    
    
    public static void main(String[] args) {
    
    
        
        Teacher t4 = new Teacher("语文", 30);//我想创建对象的过程中,我就直接赋值
        t4.print();//成员方法print
        /*System.out.println(t4.age);
        System.out.println(t4.course);*/

        /*//这么多构造方法,怎么知道调用哪一个?
        Teacher t3 = new Teacher(16);
        System.out.println(t3.age);         //16
        System.out.println(t3.course);      //null
        */
    }
}
class Teacher {
    
    
    //定义成员变量
    String course;//课程
    int age;

    //定义Teacher的构造方法
    public Teacher(){
    
    

    }
                   //定义有参的构造方法
    public Teacher(String course, int age) {
    
    
        //赋值,把方法传入的局部变量 赋值给当前对象的成员变量
        //this.course代表当前对象的course成员变量
        //后面的course是方法传入的参数 局部变量
       /* this.course = course;
        this.age = age;*/

       //使用this关键字调用后面两个单参数的构造方法
        //Call to 'this()' must be first statement in constructor body
        this(course);
        this.age = age;
        //this(age);   //error
    }
                   //定义一个单参数的构造方法
    public Teacher(String course){
    
    
        //赋值,把方法传入的局部变量 赋值给当前对象的成员变量
        this.course = course;
    }
                   //定义一个单参数的构造方法
    public Teacher(int age){
    
    
        //赋值,把方法传入的局部变量 赋值给当前对象的成员变量
        this.age = age;
    }
    
   //定义一个成员方法
   public void print(){
    
    
       System.out.println(this.course);
       System.out.println(this.age);
   }
}

Some important details of the construction method (constructor)

  1. When there is no explicit definition of the constructor in a class, this timeJVM will provide a no-argument construction method by default
  2. When in a classExplicitly define the parameter construction method, then the default no-parameter construction method will not be provided Generate后直接选择生成多个构造方法 (mac系统没找见INSERT键)

After defining a parameterized structure, develop a good habit, you must add a parameterless structure to this class
. In the
later frameworks, some frameworks will automatically call the parameterless structure to create objects

  1. The construction method is also a method, since it is a method,Can method overload
  2. Also in the constructorYou can use this keyword

1, you can call another constructor in the current class of 语法是this(参数列表)
special attention: the use of this call other constructors, the this must be the first line of the constructor, so only use one this
2, you can use this keyword to call the current object Member variables and member methods
.
Note: In the best case, let the constructor focus on creating objects, and don’t care about other things

  1. Construction methodNot an ordinary method, let alone a member method, This method is not called for programmers, butCalled to jvmThere is reflection at the back, which is equivalent to indirect use

Guess you like

Origin blog.csdn.net/AC_872767407/article/details/113406689