📜  Javathis和this()的区别

📅  最后修改于: 2021-09-12 10:40:01             🧑  作者: Mango

在Java, this 和 this() 彼此完全不同。

  • this关键字用于引用当前对象,即通过该对象调用方法。
  • this()用于从同一类的另一个调用一个构造函数。

下表显示了this 关键字this()之间的点对点差异

this this()

this keyword is used with the objects only.

this() is used with constructors only.

It refers to the current object.

It refers to the constructor of the same class whose parameters matches with the parameters passed to this(parameters).

Dot(.) operator is used to access the members. 
For example, this.variableName;

No Dot(.) operator is used. Only the matching parameters are passed.

It is used to differentiate between the local variable and the instance variable in the method.

It is used to refer to the constructor belonging to the same class.

请参阅下面的代码,其中描述了此关键字的使用。

Java
import java.io.*;
  
public class Student {
  
    private String name;
    private int age;
  
    // Note that in the Constructor below "this keyword" is
    // used to differentiate between the local variable and
    // the instance variable.
  
    public Student(String name, int age)
    {
        // Assigns the value of local name variable
        // to the name(instance variable).
        this.name = name;
        // Assigns the value of local Age variable
        // to the Age(instance variable).
        this.age = age;
    }
  
    public void show()
    {
        System.out.println("Name = " + this.name);
        System.out.println("Age = " + this.age);
    }
  
    public static void main(String[] args)
    {
        // Creating new instance of Student Class
        Student student = new Student("Geeks", 20);
        student.show();
    }
}


Java
import java.io.*;
  
public class Student {
  
    private String name;
    private int age;
  
    // Constructor 1 with String as parameter.
    public Student(String name)
    {
        // This line of code calls the second constructor.
        this(20);
        System.out.println("Name of Student : " + name);
    }
  
    // Constructor 2 with int in parameter.
    public Student(int age)
    {
        System.out.println("Age of student = " + age);
    }
  
    // Constructor 3 with no parameters.
    public Student()
    {
        // This line calls the first constructor.
        this("Geeks");
    }
  
    public static void main(String[] args)
    {
        // This calls the third constructor.
        Student student = new Student();
    }
}


输出
Name = Geeks
Age = 20

现在看看下面描述this()用法的代码

Java

import java.io.*;
  
public class Student {
  
    private String name;
    private int age;
  
    // Constructor 1 with String as parameter.
    public Student(String name)
    {
        // This line of code calls the second constructor.
        this(20);
        System.out.println("Name of Student : " + name);
    }
  
    // Constructor 2 with int in parameter.
    public Student(int age)
    {
        System.out.println("Age of student = " + age);
    }
  
    // Constructor 3 with no parameters.
    public Student()
    {
        // This line calls the first constructor.
        this("Geeks");
    }
  
    public static void main(String[] args)
    {
        // This calls the third constructor.
        Student student = new Student();
    }
}
输出
Age of student = 20
Name of Student : Geeks