this关键字

this 是自身的一个对象,代表对象本身,可以理解为:指向对象本身的一个指针。在java中,这是一个引用当前对象的引用变量

java this关键字的用法如下:

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

//更多请阅读:https://www.yiibai.com/java/this-keyword.html


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

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

Public class Student {
    int rollno;
    String name;
    float fee;

    Student(int rollno, String name, float fee) {
        this.rollno = rollno;		// this.rollno指的是Student 
        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();
    }
}
  • 局部变量:在方法、构造方法或者语句块中定义的变量被称为局部变量。

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

2. this:调用当前类方法

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

Java学习—this关键字_this关键字

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

hello n
hello m

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

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

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

new A(10) ---调用有参构造器

this()---调用无参构造器

class A {
    A() {
        System.out.println("hello a");
    }

    A(int x) {
        this();		// 调用有参构造器 A(){}
        System.out.println(x);
    }
}

class TestThis5 {
    public static void main(String args[]) {
        A a = new A(10);	// 调用有参构造器 A(int x){}
    }
}

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

hello a
10

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

new A() ---调用无参构造器

this(5)---调用有参构造器

class A {
    A() {
        this(5);	// 调用有参构造器 A(int x){}
        System.out.println("hello a");
    }

    A(int x) {
        System.out.println(x);
    }
}

class TestThis6 {
    public static void main(String args[]) {
        A a = new A();	// ---调用无参构造器 A(){}
    }
}

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

5
hello 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 constructor
        this.fee = fee;
        
        // 不把 this() 语句放在第一行,因此编译不通过
        // this(rollno, name, course);    // C.T.Error报错
    }

    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()必须是构造函数中的第一个语句。

4. 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

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

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


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






阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: Java