4.9 this 关键字
(1)this 可以出现在实例方法和构造方法中
(2)不可以出现在类方法中
4.9.1 在构造方法中使用this
this关键字出现在构造方法中的时候,代表使用该构造方法所创建的对象
class People{
String name;
People(String s){
name = s;
init();
}
void init(){
···
}
}
4.9.2 在实例方法中使用this
注意:实例方法只能通过对象来调用,不能使用类名来调用
(1)当this出现在实例方法中,this代表正在调用该方法的当前对象。this·成员变量;
(2)static成员变量出现在实例方法中的时候,默认格式:类名·成员变量;