this:
1、this是一个关键字,全部小写
2、this是什么,在内存方面是怎样的?
一个对象一个this。
this是一个变量,是一个引用。this保存当前对象的内存地址,指向自身。所以严格意义上来说,this代表的就是“当前对象”
3、this只能使用在实例方法中,谁调用这个实例方法,this就是谁。所以this代表的是:当前对象。
4、this.大部分情况下是可以省略的
5、为什么this不能用于静态方法中?
因为this代表的是当前对象,而静态方法的调用不需要对象。
public class ThisTest01{
public static void main(String[] args){
Customer c1=new Customer("张三");
c1.shopping();
Customer.shopping();
}
}
class Customer{
String name;//实例变量必须采用“引用.”的方式访问
public Customer(){
}
public Customer(String s){
name=s;
}
public void shopping(){
//c1调用shopping(),this是c1;c2调用shopping(),this是c2
//this.是可以省略的。省略的话,还是默认访问“当前对象”的name
System.out.println(this.name+"正在购物");
/* 这样是不行的
static int a =this;
System.out.println(a);
}
*/