橘子味的心
标题:Java this关键字

在java中,this关键字有很多种用法。 在java中,这是一个引用当前对象的引用变量。

java this关键字的用法如下:

  1. this关键字可用来引用当前类的实例变量。
  2. this关键字可用于调用当前类方法(隐式)。
  3. this()可以用来调用当前类的构造函数。
  4. this关键字可作为调用方法中的参数传递。
  5. this关键字可作为参数在构造函数调用中传递。
  6. this关键字可用于从方法返回当前类的实例。

建议:如果你是java初学者,只学习 this 关键字的前三个用法就可以了。

1. this:引用当前类的实例变量

this关键字可以用来引用当前类的实例变量。如果实例变量和参数之间存在歧义,则 this 关键字可用于明确地指定类变量以解决歧义问题。

了解没有 this 关键字的问题

下面先来理解一个不使用 this 关键字的示例:

  1. class Student {
  2. int rollno;
  3. String name;
  4. float fee;
  5.  
  6. Student(int rollno, String name, float fee) {
  7. rollno = rollno;
  8. name = name;
  9. fee = fee;
  10. }
  11.  
  12. void display() {
  13. System.out.println(rollno + " " + name + " " + fee);
  14. }
  15. }
  16.  
  17. class TestThis1 {
  18. public static void main(String args[]) {
  19. Student s1 = new Student(111, "ankit", 5000f);
  20. Student s2 = new Student(112, "sumit", 6000f);
  21. s1.display();
  22. s2.display();
  23. }
  24. }
  25. Java

执行上面代码输出结果如下 -

  1. 0 null 0.0
  2. 0 null 0.0
  3. Java

在上面的例子中,参数(形式参数)和实例变量(rollnoname)是相同的。 所以要使用this关键字来区分局部变量和实例变量。

使用 this 关键字解决了上面的问题

  1. class Student {
  2. int rollno;
  3. String name;
  4. float fee;
  5.  
  6. Student(int rollno, String name, float fee) {
  7. this.rollno = rollno;
  8. this.name = name;
  9. this.fee = fee;
  10. }
  11.  
  12. void display() {
  13. System.out.println(rollno + " " + name + " " + fee);
  14. }
  15. }
  16.  
  17. class TestThis2 {
  18. public static void main(String args[]) {
  19. Student s1 = new Student(111, "ankit", 5000f);
  20. Student s2 = new Student(112, "sumit", 6000f);
  21. s1.display();
  22. s2.display();
  23. }
  24. }
  25. Java

执行上面代码输出结果如下 -

  1. 111 ankit 5000
  2. 112 sumit 6000
  3. Java

如果局部变量(形式参数)和实例变量不同,则不需要像下面的程序一样使用this关键字:

不需要 this 关键字的程序示例

  1. class Student {
  2. int rollno;
  3. String name;
  4. float fee;
  5.  
  6. Student(int r, String n, float f) {
  7. rollno = r;
  8. name = n;
  9. fee = f;
  10. }
  11.  
  12. void display() {
  13. System.out.println(rollno + " " + name + " " + fee);
  14. }
  15. }
  16.  
  17. class TestThis3 {
  18. public static void main(String args[]) {
  19. Student s1 = new Student(111, "ankit", 5000f);
  20. Student s2 = new Student(112, "sumit", 6000f);
  21. s1.display();
  22. s2.display();
  23. }
  24. }
  25. Java

执行上面代码输出结果如下 -

  1. 111 ankit 5000
  2. 112 sumit 6000
  3. Java

对变量使用有意义的名称是一种好的编程习惯。所以使用相同名称的实例变量和参数,并且总是使用this关键字。

2. this:调用当前类方法

可以使用this关键字调用当前类的方法。如果不使用this关键字,编译器会在调用方法时自动添加此 this 关键字。我们来看看这个例子。

执行上面代码输出结果如下 -

  1. hello n
  2. hello m
  3. Java

3. this():调用当前类的构造函数

this()构造函数调用可以用来调用当前类的构造函数。 它用于重用构造函数。 换句话说,它用于构造函数链接。

从参数化构造函数调用默认构造函数:

  1. class A {
  2. A() {
  3. System.out.println("hello a");
  4. }
  5.  
  6. A(int x) {
  7. this();
  8. System.out.println(x);
  9. }
  10. }
  11.  
  12. class TestThis5 {
  13. public static void main(String args[]) {
  14. A a = new A(10);
  15. }
  16. }
  17. Java

执行上面代码输出结果如下 -

从默认构造函数调用参数化构造函数:

  1. class A {
  2. A() {
  3. this(5);
  4. System.out.println("hello a");
  5. }
  6.  
  7. A(int x) {
  8. System.out.println(x);
  9. }
  10. }
  11.  
  12. class TestThis6 {
  13. public static void main(String args[]) {
  14. A a = new A();
  15. }
  16. }
  17. Java

执行上面代码输出结果如下 -

使用this()构造函数调用

this()构造函数调用用于从构造函数重用构造函数。 它维护构造函数之间的链,即它用于构造函数链接。看看下面给出的示例,显示this关键字的实际使用。

  1. class Student {
  2. int rollno;
  3. String name, course;
  4. float fee;
  5.  
  6. Student(int rollno, String name, String course) {
  7. this.rollno = rollno;
  8. this.name = name;
  9. this.course = course;
  10. }
  11.  
  12. Student(int rollno, String name, String course, float fee) {
  13. this(rollno, name, course);// reusing constructor
  14. this.fee = fee;
  15. }
  16.  
  17. void display() {
  18. System.out.println(rollno + " " + name + " " + course + " " + fee);
  19. }
  20. }
  21.  
  22. class TestThis7 {
  23. public static void main(String args[]) {
  24. Student s1 = new Student(111, "ankit", "java");
  25. Student s2 = new Student(112, "sumit", "java", 6000f);
  26. s1.display();
  27. s2.display();
  28. }
  29. }
  30. Java

执行上面代码输出结果如下 -

  1. 111 ankit java null
  2. 112 sumit java 6000
  3. Java

注意:调用this()必须是构造函数中的第一个语句。

下面示例为不把 this() 语句放在第一行,因此编译不通过。

  1. class Student {
  2. int rollno;
  3. String name, course;
  4. float fee;
  5.  
  6. Student(int rollno, String name, String course) {
  7. this.rollno = rollno;
  8. this.name = name;
  9. this.course = course;
  10. }
  11.  
  12. Student(int rollno, String name, String course, float fee) {
  13. this.fee = fee;
  14. this(rollno, name, course);// C.T.Error
  15. }
  16.  
  17. void display() {
  18. System.out.println(rollno + " " + name + " " + course + " " + fee);
  19. }
  20. }
  21.  
  22. class TestThis8 {
  23. public static void main(String args[]) {
  24. Student s1 = new Student(111, "ankit", "java");
  25. Student s2 = new Student(112, "sumit", "java", 6000f);
  26. s1.display();
  27. s2.display();
  28. }
  29. }
  30. Java

执行上面代码输出结果如下 -

  1. Compile Time Error: Call to this must be first statement in constructor
  2. Java

4. this:作为参数传递给方法

this关键字也可以作为方法中的参数传递。 它主要用于事件处理。 看看下面的一个例子:

  1. class S2 {
  2. void m(S2 obj) {
  3. System.out.println("method is invoked");
  4. }
  5.  
  6. void p() {
  7. m(this);
  8. }
  9.  
  10. public static void main(String args[]) {
  11. S2 s1 = new S2();
  12. s1.p();
  13. }
  14. }
  15. Java

执行上面代码输出结果如下 -

  1. method is invoked
  2. Java

这个应用程序可以作为参数传递:

在事件处理(或)的情况下,必须提供一个类的引用到另一个。 它用于在多个方法中重用一个对象。

this:在构造函数调用中作为参数传递

也可以在构造函数中传递this关键字。 如果必须在多个类中使用一个对象,可以使用这种方式。 看看下面的一个例子:

  1. class B {
  2. A4 obj;
  3.  
  4. B(A4 obj) {
  5. this.obj = obj;
  6. }
  7.  
  8. void display() {
  9. System.out.println(obj.data);// using data member of A4 class
  10. }
  11. }
  12.  
  13. class A4 {
  14. int data = 10;
  15.  
  16. A4() {
  17. B b = new B(this);
  18. b.display();
  19. }
  20.  
  21. public static void main(String args[]) {
  22. A4 a = new A4();
  23. }
  24. }
  25. Java

执行上面代码输出结果如下 -

6. this关键字用来返回当前类的实例

可以从方法中 this 关键字作为语句返回。 在这种情况下,方法的返回类型必须是类类型(非原始)。 看看下面的一个例子:

作为语句返回的语法

  1. return_type method_name(){
  2. return this;
  3. }
  4. Java

从方法中返回为语句的 this 关键字的示例

  1. class A {
  2. A getA() {
  3. return this;
  4. }
  5.  
  6. void msg() {
  7. System.out.println("Hello java");
  8. }
  9. }
  10.  
  11. class Test1 {
  12. public static void main(String args[]) {
  13. new A().getA().msg();
  14. }
  15. }
  16. Java

执行上面代码输出结果如下 -

验证 this 关键字

现在来验证 this 关键字引用当前类的实例变量。 在这个程序中将打印参考变量,这两个变量的输出是相同的。

  1. class A5 {
  2. void m() {
  3. System.out.println(this);// prints same reference ID
  4. }
  5.  
  6. public static void main(String args[]) {
  7. A5 obj = new A5();
  8. System.out.println(obj);// prints the reference ID
  9. obj.m();
  10. }
  11. }
  12. Java

执行上面代码输出结果如下 -

  1. A5@22b3ea59
  2. A5@22b3ea59
  3. Java