this
打印一个对象,输出的是这个对象的地址(JDK 提供的地址,非实际内存地址)
hashcode唯一标识
class Dog{
private String name;
public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
return;
}
public void run(){
System.out.println("run --------------" + this);
}
}
public static void main(String[] args){
Dog dog = new Dog();
System.out.println("main-------------"+dog);
dog.run();
}
运行结果:
main-------------Dog@7852e922
run --------------Dog@7852e922
this 只能在类的对象方法中使用
this 代表当前调用这个this所在的方法的对象的自身
this 可以在方法内区分同名的类的属性和变量参数名,有this的一定是属性