Java this关键字

本文概述

  • 使用java this关键字
可以有很多java this关键字的用法。在java中,this是一个引用变量,是指当前对象。
使用java this关键字这是考虑到6使用java this关键字。
  1. this可以用来表示当前类的实例变量。
  2. this可以用于调用当前类方法(隐式地)
  3. this()可以用于调用当前类的构造函数。
  4. this可以作为参数传递的方法调用。
  5. this可以作为参数传递给构造函数调用。
  6. this可以用来返回当前类的实例方法。
建议:如果你是初学者到java,查找该关键字的只有三个用法。
1):引用当前类的实例变量
可以使用this关键字引用当前类的实例变量。如果有实例变量和参数之间的歧义,this关键字解析模棱两可的问题。
理解没有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.0 0 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,"ankit",5000f); Student s2=new Student(112,"sumit",6000f); s1.display(); s2.display(); }}

【Java this关键字】输出:
111 ankit 5000 112 sumit 6000

如果局部变量(正式的参数)和实例变量不同,没有必要使用this关键字在以下项目:
程序在this关键字不是必需的
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,"ankit",5000f); Student s2=new Student(112,"sumit",6000f); s1.display(); s2.display(); }}

输出:
111 ankit 5000 112 sumit 6000

更好的方法是对变量使用有意义的名称。所以我们使用相同名称的实例变量和参数实时,总是使用this关键字。2):调用当前类方法
你可以调用当前类的方法通过使用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 n hello 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 a 10

从默认的构造函数调用参数化构造函数:
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(); }}

输出:
5 hello 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); //reusing constructor 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,"ankit","java"); Student s2=new Student(112,"sumit","java",6000f); s1.display(); s2.display(); }}

输出:
111 ankit java null 112 sumit java 6000

规则:调用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); //C.T.Error } void display(){System.out.println(rollno+" "+name+" "+course+" "+fee); } } class TestThis8{ public static void main(String args[]){ Student s1=new Student(111,"ankit","java"); Student s2=new Student(112,"sumit","java",6000f); s1.display(); s2.display(); }}

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

4):通过this作为参数的方法
this关键字也可以作为参数传递的方法。它主要用于事件处理。让我们看看this例子:
class S2{ void m(S2 obj){ System.out.println("method is invoked"); } void p(){ m(this); } public static void main(String args[]){ S2 s1 = new S2(); s1.p(); } }

输出:
method is invoked

应用this可以作为参数传递:
在事件处理(或)的情况我们已经向另一个类的提供参考。它是用来在许多方法重用一个对象。
5):通过构造函数调用的参数
我们可以通过this关键词也在构造函数中。是有用的,如果我们需要使用多个类的一个对象。让我们看看这个例子:
class B{ A4 obj; B(A4 obj){ this.obj=obj; } void display(){ System.out.println(obj.data); //using data member of A4 class } }class A4{ int data=http://www.srcmini.com/10; A4(){ B b=new B(this); b.display(); } public static void main(String args[]){ A4 a=new A4(); } }

Output:10

6)this可以用来返回当前的类实例
我们可以返回this关键字声明的方法。在这种情况下,该方法的返回类型必须是类类型(销售)。让我们看看这个例子:
语法,可以作为一份声明返回
return_type method_name(){ return this; }

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关键字
让我们证明this关键字引用当前类实例变量。在这个程序中,我们打印参考变量,这两个变量的输出是相同的。
class A5{ void m(){ System.out.println(this); //prints same reference ID } public static void main(String args[]){ A5 obj=new A5(); System.out.println(obj); //prints the reference ID obj.m(); } }

输出:
A5@22b3ea59 A5@22b3ea59

    推荐阅读