在Java中使用"this"运算符

Viv*_*ani 0 java

this除了访问与局部变量同名的成员变量之外,是否还有其他关键字用法

this.x = x

是否有任何其他情况下使用此关键字是有意义的.

aqu*_*aga 9

this关键字的另一个用途是构造函数链接,例如:

class Person {

    private String name;
    private int age;

    public Person() {
        //Invoking another constructor
        this("John", 35);
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
}
Run Code Online (Sandbox Code Playgroud)


Mik*_*uel 6

您可以将当前对象作为参数传递给另一个方法.