Java教程

Java this关键字

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

java this关键字的用法

java this关键字的6种用法。
这可以用来引用当前的类实例变量。 这可用于(隐式)调用当前的类方法 this()可用于调用当前的类构造函数。 这可以作为方法调用中的参数传递。 这可以在构造函数调用中作为参数传递。 这可用于从方法中返回当前的类实例。
建议: : 如果您是Java的初学者,请仅查找this关键字的三种用法。
使用Java这个关键字

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

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.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,"ankit",5000f);
        Student s2=new Student(112,"sumit",6000f);
        s1.display();
        s2.display();
    }
}
输出:
111 ankit 5000112 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 5000112 sumit 6000
注意: 对变量使用有意义的名称是一种更好的方法。因此,我们对实例变量和参数实时使用相同的名称,并始终使用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()构造函数调用应用于重用构造函数中的构造函数。它维护了构造函数之间的链,即用于构造函数链接。让我们看下面给出的示例,该示例显示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 constructorthis.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 null112 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关键字也可以作为方法中的参数传递。它主要用于事件处理。让我们来看一个例子:
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): 在构造函数调用中作为参数传递

我们可以在构造函数也。如果我们必须在多个类中使用一个对象,这将很有用。让我们来看一个例子:
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=10;
    A4(){
        B b=new B(this);
        b.display();
    }
    public static void main(String args[]){
        A4 a=new A4();
    }
}
Output:10

6)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 IDobj.m();
    }
}
输出:
A5@22b3ea59A5@22b3ea59
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4