查看原文
其他

【Java编程教程】详解Java this 关键字

点击关注👉 Java面试那些事儿 2023-09-01

整理:Java面试那些事儿


Java this关键字可以有很多用法。在 Java 中,这是引用当前对象的引用变量。



# Java this 关键字的用法


下面给出java this关键字的6种用法。


  1. this 可用于引用当前类实例变量。

  2. this 可用于调用当前类方法(隐式)

  3. this() 可用于调用当前类构造函数。

  4. this 可以作为方法调用中的参数传递。

  5. this 可以在构造函数调用中作为参数传递。

  6. this 可用于从方法返回当前类实例。


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


this 关键字可用于引用当前类实例变量。如果实例变量和参数之间存在歧义,则此关键字可解决歧义问题。


理解没有这个关键字的问题


让我们通过下面给出的例子来理解如果我们不使用这个关键字的问题:

class Student{ int rollno; String name; float fee; Student(int rollno,String name,float fee){ rollno=rollno; name=name; fee=fee; } void display(){System.out.println(rollno+" "+name+" "+fee);} } class TestThis1{ public static void main(String args[]){ Student s1=new Student(111,"ankit",5000f); Student s2=new Student(112,"sumit",6000f); s1.display(); s2.display(); }}


输出:

0 null 0.00 null 0.0


在上面的示例中,参数(形式参数)和实例变量是相同的。因此,我们使用 this 关键字来区分局部变量和实例变量。


# 通过this关键字解决上述问题


class Student{ int rollno; String name; float fee; Student(int rollno,String name,float fee){ this.rollno=rollno; this.name=name; this.fee=fee; } void display(){System.out.println(rollno+" "+name+" "+fee);} }
class TestThis2{ public static void main(String args[]){ Student s1=new Student(111,"小安",5000f); Student s2=new Student(112,"小宋",6000f); s1.display(); s2.display(); }}


输出:

111 小安 5000.0112 小宋 6000.0


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


专属福利

👉点击领取:651页Java面试题库


不需要此关键字的程序


class Student{ int rollno; String name; float fee; Student(int r,String n,float f){ rollno=r; name=n; fee=f; } void display(){System.out.println(rollno+" "+name+" "+fee);} }
class TestThis3{ public static void main(String args[]){ Student s1=new Student(111,"小安",5000f); Student s2=new Student(112,"小宋",6000f); s1.display(); s2.display(); }}


输出:

111 小安 5000.0112 小宋 6000.0


为变量使用有意义的名称是更好的方法。所以我们实时对实例变量和参数使用相同的名称,并且始终使用 this 关键字。


2)this:调用当前类方法


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

class A{ void m(){System.out.println("hello m");} void n(){ System.out.println("hello n"); //m();//same as this.m() this.m(); } } class TestThis4{ public static void main(String args[]){ A a=new A(); a.n(); }}


输出:

hello nhello m


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


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


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

class A{ A(){System.out.println("hello a");} A(int x){ this(); System.out.println(x); } } class TestThis5{ public static void main(String args[]){ A a=new A(10); }}


输出:

hello a10


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

class A{ A(){ this(5); System.out.println("hello a"); } A(int x){ System.out.println(x); } } class TestThis6{ public static void main(String args[]){ A a=new A(); }}


输出:

5hello a


# this() 构造函数调用的实际用法


应该使用 this() 构造函数调用来重用构造函数中的构造函数。它维护构造函数之间的链,即它用于构造函数链接。让我们看看下面给出的示例,该示例显示了此关键字的实际用法。


class Student{ int rollno; String name,course; float fee; Student(int rollno,String name,String course){ this.rollno=rollno; this.name=name; this.course=course; } Student(int rollno,String name,String course,float fee){ this(rollno,name,course);//重用构造函数 this.fee=fee; } void display(){System.out.println(rollno+" "+name+" "+course+" "+fee);} } class TestThis7{ public static void main(String args[]){ Student s1=new Student(111,"小安","java"); Student s2=new Student(112,"小宋","java",6000f); s1.display(); s2.display(); }}


输出:

111 小安 java 0.0112 小宋 java 6000.0


规则:对 this() 的调用必须是构造函数中的第一条语句。


class Student{ int rollno; String name,course; float fee; Student(int rollno,String name,String course){ this.rollno=rollno; this.name=name; this.course=course; } Student(int rollno,String name,String course,float fee){ this.fee=fee; this(rollno,name,course);//CT错误 } void display(){System.out.println(rollno+" "+name+" "+course+" "+fee);} } class TestThis8{ public static void main(String args[]){ Student s1=new Student(111,"小安","java"); Student s2=new Student(112,"小宋","java",6000f); s1.display(); s2.display(); }}


输出:

Compile Time Error: Call to this must be first statement in constructor


4) this: 在方法中作为参数传递


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


class S2{ void m(S2 obj){ System.out.println("方法被调用"); } void p(){ m(this); } public static void main(String args[]){ S2 s1 = new S2(); s1.p(); } }


输出:

方法被调用


# 可以作为参数传递的 this 的应用:


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


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


我们也可以在构造函数中传递 this 关键字。如果我们必须在多个类中使用一个对象,这很有用。让我们看看这个例子:

class B{ A4 obj; B(A4 obj){ this.obj=obj; } void display(){ System.out.println(obj.data); //使用A4类的数据成员 } } class A4{ int data=10; A4(){ B b=new B(this); b.display(); } public static void main(String args[]){ A4 a=new A4(); } }


输出:

10


6) this关键字可用于返回当前类实例


我们可以将此关键字作为方法的语句返回。在这种情况下,方法的返回类型必须是类类型(非原始类型)。让我们看看这个例子:


可以作为语句返回的 this 的语法


return_type method_name(){ return this; }


作为方法语句返回的关键字示例


class A{ A getA(){ return this; } void msg(){System.out.println("Hello java");} } class Test1{ public static void main(String args[]){ new A().getA().msg(); } }


输出:

Hello java


# 证明这个关键字


让我们来证明这个关键字引用了当前的类实例变量。在这个程序中,我们打印引用变量和 this,两个变量的输出是相同的。

class A5{ void m(){ System.out.println(this);//打印相同的参考ID } public static void main(String args[]){ A5 obj=new A5(); System.out.println(obj);//打印参考ID obj.m(); } }


输出:

A5@22b3ea59
A5@22b3ea59



程序员技术交流群


扫码进群记得备注:城市、昵称和技术方向


热门推荐:


您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存