Java 关键字 this 使用详解(通俗易懂)

07-06 1109阅读

this关键字主要有以下三个地方使用

        在方法体中引用当前对象,即其方法被调用的对象,以便将当前对象的实例变量或当前对象作为参数传递给其他方法。

① t = this.x;  要在方法中引用当前对象,可以使用关键字 this。

② return this;  作为当前方法的返回值等。

③ z.resetData(this); 关键字 this 指向当前对象,可用于任何可使用对象引用的地方:在句点表示法中,作为方法的参数;

下面是一些使用 this 的例子,其中的注释对相应的用法做了说明。

一、t = this.x;

this 用于指代当前对象,而 .x 则是该对象的属性。因此,this.x 表示当前对象的属性 x

public class MyClass {
    private int x;
    
    public MyClass(int x) {
        this.x = x; // 设置当前对象的属性 x
    }
    
    public int getX() {
        return this.x; // 返回当前对象的属性 x
    }
    
    public static void main(String[] args) {
        MyClass obj = new MyClass(10);
        int t = obj.getX(); // 使用 this.x 赋值给变量 t
    }
}

还有在内部类中,使用this关键字引用外部类对象。

public class OuterClass {
    private int value;
 
    public void outerMethod() {
        InnerClass inner = new InnerClass();
        inner.innerMethod();
    }
 
    public class InnerClass {
        public void innerMethod() {
            OuterClass.this.value = 10; // 使用this关键字引用外部类对象的成员变量
        }
    }
}

二、return this;

return this; 表示从当前方法中返回当前对象的引用,即返回指向当前对象的指针或引用。

当一个方法的返回类型是类本身(或该类的父类或接口),而不是基本数据类型或其他对象类型时,可以使用 return this; 来返回当前对象的引用。

例如,在一个类的方法中,如果你想要返回调用该方法的对象本身,而不是其他值,你可以使用 return this;。这在链式调用或者需要返回当前对象的场景中特别有用。

public class Leaf {
    public static void main(String[] args){
        Leaf leaf = new Leaf();
        leaf.increment().increment().increment().print();
    }
    int i = 0;
     Leaf increment(){
         i++;
         return  this;
     }
     void print(){
         System.out.println("i = " + i);
     }
}

Java 关键字 this 使用详解(通俗易懂)

increment方法用于递增实例变量i的值并返回当前对象的引用,以便支持方法链的形式调用,实现了在一个语句中对同一个对象进行了多次操作。

三、Peeler.peel(this);

Apple 需要调用 Peeler.peel() 方法,它是一个外部的工具方法,为了将自身传递给外部方法,Apple必须使用 this 关键字。

/**
 * 这段代码实现了一个简单的场景,演示了对象间的方法调用和 toString 方法的使用。
 * 在 main 方法中创建了一个 Person 对象,并调用其 eat 方法,传入一个新创建的 Apple 对象。
 * eat 方法内部调用了传入的 Apple 对象的 getPeeled 方法,获取削皮后的苹果对象。
 * getPeeled 方法内部调用了 Peeler 类的 peel 静态方法,对当前苹果对象进行削皮操作,并返回结果。
 * 削皮操作完成后,削皮后的苹果对象被打印输出到控制台。
 */
public class PassingThis {
    public static void main(String[] args) {
        //在 main 方法中创建了一个 Person 对象,并调用其 eat 方法,传入一个新创建的 Apple 对象。
        new Person().eat(new Apple());
    }
}
class Person {
    // Person 类中定义了一个 eat 方法,接受一个 Apple 对象作为参数。调用传入的 Apple 对象的 getPeeled 方法,获取削皮后的苹果对象,然后打印输出这个削皮后的苹果对象。
    public void eat(Apple apple) {
        Apple peeled = apple.getPeeled();
        // 直接输出 peeled 对象,会调用其 toString 方法
        System.out.println("这个苹果是:" + peeled);
    }
}
class Peeler {
    static Apple peel(Apple apple) {
        // Peeler 类中定义了一个静态方法 peel,接受一个 Apple 对象作为参数,并对这个苹果对象进行削皮操作,削皮操作非常简单,只是直接返回传入的苹果对象,没有进行实际的削皮操作。
        return apple;
    }
}
class Apple {
    Apple getPeeled() {
        // Apple 类中定义了一个 getPeeled 方法,它返回一个经过削皮后的苹果对象。
        // 在这个方法中,通过调用 Peeler 类的 peel 静态方法来实现对当前苹果对象的削皮操作,并将结果返回。
        return Peeler.peel(this);
    }
    @Override
    public String toString() {
        // 在 Apple 类中重写了 toString 方法,使其返回一个描述削皮后苹果的字符串,这样在打印输出时就会显示你自定义的描述,而不是默认的类名和哈希码。
        return "削皮后的苹果";
    }
}

Java 关键字 this 使用详解(通俗易懂)

注意:

this 关键字只能在方法内部使用,表示对“调用方法的那个对象”的引用。同时,如果在方法内部调用同一个类的同一个方法,就直接调用,不用加this。

public class Example {
    private int value;
    public Example(int value) {
        this.value = value;
    }
    // 方法内部使用this关键字引用当前对象,并返回当前对象的值
    public int getValue() {
        return this.value;
    }
    // 方法内部调用同一个类的同一个方法,不需要加this
    public void printValue() {
        int val = getValue(); // 直接调用getValue方法
        System.out.println("Value: " + val);
    }
    public static void main(String[] args) {
        Example example = new Example(10);
        example.printValue();
    }
}

        这个示例中,Example类有一个私有成员变量value和两个公共方法getValue和printValue。在getValue方法中,使用了this关键字来引用当前对象,并返回value的值。而在printValue方法中,直接调用了getValue方法,不需要加this关键字。在main方法中,创建了Example类的实例对象,并调用了printValue方法,输出value的值。

参考:

Java 关键字 this 使用详解(通俗易懂)

VPS购买请点击我

文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

目录[+]