Home > Java > javaTutorial > This reference and object construction initialization methods in java

This reference and object construction initialization methods in java

WBOY
Release: 2023-05-14 18:40:06
forward
1007 people have browsed it

    1. this reference

    1.1 Why is there a this reference

    Let’s write one first Example of date class:

    public class classCode {
        public int year;
        public int month;
        public int day;
        public void setDay(int y, int m, int d){
            year = y;
            month = m;
            day = d;
        }
        public void printDate(){
            System.out.println(year + "-" + month + "-" + day);
        }
        public static void main(String[] args) {
            // 构造三个日期类型的对象 d1 d2 d3
            classCode Date1 = new classCode();
            classCode Date2 = new classCode();
            classCode Date3 = new classCode();
    
            // 对d1,d2,d3的日期设置
            Date1.setDay(2022, 8, 9);
            Date2.setDay(2023, 8, 9);
            Date3.setDay(2024, 8, 9);
    
            //打印日期的内容
            Date1.printDate();
            Date2.printDate();
            Date3.printDate();
        }
    }
    Copy after login

    This reference and object construction initialization methods in java

    The above code defines a date class, and then creates three objects in the main method and passes them through the member methods in the classCode class To set and print objects, the overall logic of the code is very simple and there are no problems.

    There are two things to note:

    1. The formal parameter name is accidentally the same as the member variable name:

    public void setDay(int year, int month, int day){ 
        year = year;
        month = month;
        day = day; 
    }
    Copy after login

    Who assigns value to whom in the function body? Member variable to member variable? Parameter to parameter? Parameters to member variables? Member variable parameters?

    2. All three objects are calling the setDate and printDate functions, but there is no description of the objects in these two functions. How do the setDate and printDate functions know which object's data is being printed?

    This reference and object construction initialization methods in java

    You can see that if the formal parameter name and member variable name are different, the value of the variable after assignment is 0, indicating that the assignment was not successful.

    What should we do? Look down.

    1.2 What is this reference

    This reference points to the current object (the object that calls the member method when the member method is running). All member variable operations in the member method are through this reference. Go visit . It’s just that all operations are transparent to the user, that is, the user does not need to pass it, the compiler automatically completes it.

    Improved code:

    public void setDay(int year, int month, int day){
        this.year = year;
        this.month = month;
        this.day = day;
    }
    Copy after login

    This reference and object construction initialization methods in java

    You can see that after adding the this reference, the assignment is successful.
    this is added by default. Even if this is not added, there will be a this added by default. But if you don't add it, it will cause problems if the formal parameter name and the member variable name are the same.

    In the following code, you can see that the three objects are calling the setDate and printDate functions, and there is no explanation, so how do you know which one is being printed? Object data?

    public static void main(String[] args) {
         // 构造三个日期类型的对象 Date1 Date2 Date3
         Date Date1 = new Date();
         Date Date2 = new Date();
         Date Date3 = new Date();
    
         // 对Date1,Date2,Date3的日期设置
         Date1.setDay(2022, 8, 9);
         Date2.setDay(2023, 8, 9);
         Date3.setDay(2024, 8, 9);
    
         //打印日期的内容
         Date1.printDate();
         Date2.printDate();
         Date3.printDate();
    }
    Copy after login

    You can judge which object to print by the following two points:

    • Which object is the previous object and which object’s data is printed?

    • Hidden parameters.

    public void setDay(Date this, int y, int m, int d){
        this.year = y;
        this.month = m;
        this.day = d;
    }
    public void printDate(Date this){
        System.out.println(this.year +"-"+ this.month +"-"+ this.day);
    }
    Copy after login

    Three ways to use this:

    • this. Member variable

    • this.Access member method

    • this();Access constructor method

    1.3 Characteristics of this reference

    • Type of this: Corresponding class type reference, that is, which object is called is the reference class of that object.

    • This can only be used in "member methods".

    • In "member methods", this can only refer to the current object and cannot refer to other objects.

    • This is the first hidden parameter of the "member method", which will be automatically passed by the compiler. When the member method is executed, the compiler will be responsible for calling the member method.

    The reference of the object is passed to the member method, and this is responsible for receiving it.

    Even if the member variable name is different from the formal parameter name, it is recommended to write this. This is equivalent to a protective measure, and it is also a good programming practice.

    1.4 This reference exercise question

    Write an academic class with attributes such as name, age, etc., and then set the values ​​​​of these attributes through a method, and then write two methods, in one method Use this to call another method.

    public class Student {
        public String name;
        public int age;
    
        public void setStudent(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        public void printStudent() {
            System.out.println(this.name + "->" + this.age);
        }
    
        public static void main(String[] args) {
            Student student = new Student();
            student.setStudent("zhangsan", 19);
            student.printStudent();
        }
    }
    Copy after login

    This reference and object construction initialization methods in java

    2. Construction and initialization of objects

    2.1 How to initialize objects

    Through the study of the previous knowledge points, we know that in Java methods When a local variable is defined internally, it must be initialized, otherwise the compilation will fail.

    public static void main(String[] args) {
        int a;
        System.out.println(a);
    }// Error:(26, 28) java: 可能尚未初始化变量a.
    Copy after login

    If it is an object, no error will be reported even if it is not assigned a value, because it is a reference variable.

     public static void main(String[] args) {
            // 构造一个日期类型的对象 
            Date date = new Date();
            date.setDay(2022, 8, 9);
            //打印日期的内容
            date.printDate();
        }//代码可以正常通过编译
    Copy after login

    Through the above examples, two problems were discovered:

    • It is troublesome to call the setDate method to set the specific date after each object is created. How should the object be initialized?

    • Local variables must be initialized before they can be used. Why can they still be used without giving a value after the field is declared?

    This introduces the construction method. Then look down.

    2.2 Constructor method

    2.2.1 Concept

    The constructor method (also called constructor) is a special member method. The name must be the same as the class name. When creating It is automatically called by the compiler when an object is created, and is only called once during the entire object's life cycle.

    public class Student {
        public String name;
        public int age;
        public Student(){//这是一个构造方法
            System.out.println("不带参数的构造方法");
        }
        public Student(String name, int age) {//这是一个构造方法
            System.out.println("带参数的构造方法");
            this.name = name;
            this.age = age;
        }
        public void setStudent(String name, int age) {
            this.name = name;
            this.age = age;
        }
        public void printStudent() {
            System.out.println(this.name + "->" + this.age);
        }
        public static void main(String[] args) {
            Student student = new Student();//这一行是构造方法的调用
        }
    }
    Copy after login

    This reference and object construction initialization methods in java

    Student student = new Student();
    new在实例化对象,而实例化对象一定会调用构造方法。

    注意:当我们没有提供构造方法时,编译器会自动提供一个不带参数的构造方法。

    2.2.2 特性
    • 名字必须与类名相同。

    • 没有返回值类型,设置为void也不行。

    • 创建对象时由编译器自动调用,并且在对象的生命周期内只调用一次。

    • 构造方法可以重载(用户根据自己的需求提供不同参数的构造方法。

     public Student(){//不带参数的构造方法
            System.out.println("不带参数的构造方法");
        }
        public Student(String name, int age) {//带两个参数的构造方法
            System.out.println("带参数的构造方法");
            this.name = name;
            this.age = age;
    
        }
    Copy after login

    上述两个构造方法:名字相同,参数列表不同,因此构成了方法重载。

    如果用户没有显式定义,编译器会生成一份默认的构造方法,生成的默认构造方法一定是无参的。

    public class Work {
        public int one = 1;
        public int three = 3;
        public int two = 2;
    
        public void printWork() {
            System.out.println(one + "-" + two + "-" + three);
        }
    
        public static void main(String[] args) {
            Work work = new Work();
            work.printWork();
        }
    }
    Copy after login

    This reference and object construction initialization methods in java

    上述Work类中,没有定义任何构造方法,编译器会默认生成一个不带参数的构造方法。

    那如何调用带参数的构造方法呢?

    public class Work {
        public int one = 1;
        public int three = 3;
        public int two = 2;
    
        public Work(int one, int two, int three) {
            System.out.println("带参数的构造方法");
            this.one = one;
            this.two = two;
            this.three = three;
        }
    
        public void printWork() {
            System.out.println(one + "-" + two + "-" + three);
        }
    
        public static void main(String[] args) {
            Work work = new Work(3, 6, 9);
            work.printWork();
        }
    }
    Copy after login

    This reference and object construction initialization methods in java

    注意:一旦用户定义,编译器则不再生成。

    构造方法中,可以通过this调用其他构造方法来简化代码。

     public Work() {
            this(10, 20, 30);//调用本类当中其他的构造方法
            System.out.println("不带参数的的构造方法");
        }
    
        public Work(int one, int two, int three) {
            System.out.println("带参数的构造方法");
            this.one = one;
            this.two = two;
            this.three = three;
        }
    Copy after login

    注意:

    • this调用必须在构造方法里面,

    • 要在在第一行,

    • 不能写成循环调用。

    绝大多数情况下使用public来修饰,特殊场景下会被private修饰(后序讲单例模式时会遇到)

    2.3 默认初始化

    为什么使用成员变量不需要初始化呢?

    This reference and object construction initialization methods in java

    在程序层面只是简单的一条语句,在JVM(以后讲)层面需要做好多事情,下面简单介绍下:

    • 检测对象对应的类是否加载了,如果没有加载则加载

    • 为对象分配内存空间

    • 处理并发安全问题

    比如:多个线程同时申请对象,JVM要保证给对象分配的空间不冲突初始化所分配的空间
    即:对象空间被申请好之后,对象中包含的成员已经设置好了初始值

    比如:

    This reference and object construction initialization methods in java

    设置对象头信息(关于对象内存模型后面会介绍)调用构造方法,给对象中各个成员赋值

    2.4 就地初始化

    定义成员变量的时候就已经赋值好了。

    public class HardWork {
        public int a = 10;//就地初始化
        public int b = 20;//就地初始化
        public String c = "zhangsan";//就地初始化
    
        public void setWork(int a,  int b, String c) {
            this.a = a;
            this.b = b;
            this.c = c;
        }
    
        public void printWork() {
            System.out.println(a + "-" + b + "-" + c);
        }
        public static void main(String[] args) {
            HardWork work = new HardWork();
            work.printWork();
            System.out.println();
        }
    }
    Copy after login

    This reference and object construction initialization methods in java

    注意:代码编译完成后,编译器会将所有给成员初始化的这些语句添加到各个构造函数中。

    The above is the detailed content of This reference and object construction initialization methods in java. For more information, please follow other related articles on the PHP Chinese website!

    Related labels:
    source:yisu.com
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template