this

用于指代当前对象的实例

使用方式

1、指代成员变量

同一类中,成员变量和方法内的局部变量重名时,用来在方法中指代成员变量

public class Demo{
    public String name;
    public int age;   //成员变量
  
    public Demo(String name, int age){   //方法变量
        this.name = name;
        this.age = age;     
    }
}

2、指代成员方法

public class DetectEngine{ 
   public DetectEngine() {
        this(Runtime.getRuntime().availableProcessors());
    }
    public DetectEngine(int instanceNum) {
         业务逻辑等
    }
}

 

posted @ 2024-08-09 16:58  zhegeMaw  阅读(19)  评论(0)    收藏  举报