package ChuanCan;
/*
*关于java中的关键字: this
* 1.this是一个引用,this 是一个变量,this变量中保存了内存地址
* 指向了它本身,this储存在JVM推内存java对象内部
*
*
* 2.每一个java对象,每一个对象中就有一个this.
* **重点:没有static关键字的方法被称为"实例方法" 引用.方法名
* **重点:没有static关键字的变量被称为"实例变量" 引用.变量名
* 当一个行为/动作执行的过程当中是需要对象参与的,那么这个方法
* 一定要定义为"实例方法",不带static关键字.
4this可以出现在"实例方法"当中,this指向当前正在执行这个动作的对象.
5.this 在对数情况下多是可以生省略不写的
**/
public class GuanJianZahiTHIS {
public static void main(String[] args) {
System.out.println("HelloWorld!");
Customer c1 = new Customer();
c1.name = "张三";
Customer c2 = new Customer();
c2.name = "李四";
c2.shipping();
//调用doOther方法
Customer.doIther();//类名点方法名
}
}
class Customer {
String name;
public void shipping(){
//System.out.println(name+ "在购物");
//完整写法
System.out.println(this.name+ "在购物");
}
//带有static
public static void doSmo(){
//这个执行过程中没有"当前对象",因为带有static的方法是通过类方式访问的
//或者说这个"上下文"当中没有"当前对象",自然也不存在this(this代表的是当前正在执行这个动作的对象)
//name是一个"实例变量",以下代码的含义是:访问当前对象的name,没有当前对象自然也不可能访问当前对象name
//***this不能用在有static关键词里
//static的方法调用不需要对象,直接是使用类名,所以执行过程中没有当前对象,所以不能使用this.
//System.out.pritnln(this.name);
}
public static void doIther(){ //那个么有static的方法怎么访问name?
//创建对象
Customer c = new Customer();
System.out.println(c.name);//这里访问的name是c引用指向对象的name
}
}
关于java中this关键字的认识
最新推荐文章于 2023-09-03 08:23:42 发布