this关键字
this关键字的值等于当前对象的地址,示例如下:
public class ThisDemo {
public static void main(String[] args) {
Car car = new Car();
System.out.println("car的值为:"+car);
car.setName("奔驰");
}
}
public class Car {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
System.out.println("this的值为:"+this);
this.name = name;
}
}
运行结果:
car的值为:com.itheima.oop.Car@682a0b20
this的值为:com.itheima.oop.Car@682a0b20
由上面的运行结果可以知道,this的值就是等于当前对象的地址值。