1、this 关键字
1.1 this 关键字的定义和性质
1)this 是一个关键字 / 引用 / 变量,this 变量中保存了内存地址表示自身,this 存储在 JVM 堆内存 java 对象内;
2)创建 100 个 java 对象,每一个对象都有 this,也就是说有 100 个不同的 this;
3)this 可以出现在“实例方法”当中,this 指向当前郑正在指向这个动作的对象。【this 代表当前对象】;
4)this 在多数情况下都是可以省略不写的;
5)this 在区分局部变量和实例变量的时候,"this."不能省;
public void setName(String name) { this.name = name; }//“=”前面的this.name是实例变量//“=”前面的name是局部变量,是形式参数
复制代码
点击并拖拽以移动
6)this 不能使用在带有 static 的方法当中;
1.2 实例方法、实例变量
1.2.1 实例方法的访问(不带 static 的方法)
public class helloworld { public static void main(String[] args) { // 创建Customer对象 Customer c1 = new Customer(); c1.name = "zhangshan"; // c1购物 c1.shopping(); // 创建Customer对象 Customer c2 = new Customer(); c2.name = "lisi"; // c2购物 c2.shopping(); }}class Customer { // 姓名 String name; // 构造方法 public Customer() { }
//实例方法 public void shopping(){ //当张三在购物的时候输出"张三在购物" //当李四在购物的时候输出"李四在购物" System.out.println(this.name + "在购物"); }}
复制代码
点击并拖拽以移动
点击并拖拽以移动
编辑
1.2.2 实例变量的访问(不带 static 的变量)
public class helloworld { public static void main(String[] args) { //访问"当前对象"的num属性 //System.out.println(num); //编译错误(因为num是一个实例变量,需要"引用."的方式访问) //如何访问num? //创建一个对象 ThisTest tt = new ThisTest(); System.out.println(tt.num); }}
class ThisTest{ //实例变量 int num = 10;}
复制代码
点击并拖拽以移动
1.3 静态方法
1)访问的时候采用 类名.方法名,不能用 this;
2)静态方法不能"直接"访问实例变量和实例方法;(实例变量和实例方法都需要对象的存在);
3)静态方法中没有 this,也就是说"当前对象"不存在,自然也无法访问当前对象的实例变量和实例方法(this 代表的是当前正在执行这个动作的对象);
4)静态方法,既可以采用类名的方式访问,也可以采用应用的方式访问,但是即使采用引用的方式访问,实际上执行的时候和引用指向的对象无关,还是默认成“类名.”的方式访问,且会产生警告,但不会产生空指针异常;
1.4 特例:实例方法中实例方法的访问
实例方法和静态方法的访问 比较
public class ThisTest{ public static void main(String[] args){ //ThisTest.doSome(); //编译错误(实例方法必须先创建对象,通过引用.的方式访问)如下: ThisTest tt = new ThisTest(); tt.run(); doSome();//带有static的方法,可以通过类名.的方式直接访问 } public static void doSome(){ System.out.println("do some!"); } public void doOther(){ System.out.println("do other"); } //特例:实例方法中实例方法的访问 public void run(){ System.out.println("run execute!"); doOther();//this.doOther //这里可以直接写“doOther();”由于是通过run()来访问到doOther的,此时this就是访问run()时的对象 }}
复制代码
点击并拖拽以移动
1.4 实例方法和实例变量的定义和访问的比较
不带有 static 关键字的方法被称为"实例方法";
不带有 static 关键字的变量被称为"实例变量";
public class ThisTest{ public static void main(String[] args){ //编译错误 /*System.out.println(name); doSome(); System.out.println(this.name); this.doSome();*/ //编译通过 ThisTest tt = new ThisTest(); System.out.println(tt.name); tt.doSome(); } //实例变量 String name; //实例方法 public void doSome(){ System.out.println("do some!"); }}
复制代码
点击并拖拽以移动
1.5 this 的作用领域
1)可以使用在实例方法中,代表当前对象【语法格式:this】
2)可以使用在构造方法中,通过当前的构造方法调用其他的构造方法【语法格式:this(实参)】;
3)this()这种语法只能出现在构造方法的第一行;
使用在构造方法中,通过当前的构造方法调用其他的构造方法的代码如下:
public class helloworld { public static void main(String[] args) { Date d = new Date(); System.out.println(d.getYear() + "-" + d.getMonth() + "-" + d.getDay()); Date a = new Date(2021,1,3); System.out.println(a.getYear() + "-" + d.getMonth() + "-" + d.getDay()); }}class Date{ private int year; private int month; private int day; public Date(int year, int month, int day) { this.year = year; this.month = month; this.day = day; } public Date() { //需求:当程序员调用以下无参数的构造方法的时候,默认创建日期为1970.1.1 /*this.year = 1970; this.month = 1; this.day = 1;*/ //以上代码可以通过调用另一个构造方法来完成 //但前提是不能通过创新新的对象。以下代码表示创新了一个新的对象 //new Date(1970.1.1); //需要采用以下的语法来完成构造方法的调用 this(1970,1,1); } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public int getMonth() { return month; } public void setMonth(int month) { this.month = month; } public int getDay() { return day; } public void setDay(int day) { this.day = day; }}
复制代码
点击并拖拽以移动
1.6 两种方法调用 以及 实例变量的访问 的完整/省略写法
public class Test{ //没有static的变量 int i = 10; //带有static的方法 public static void doSome() { System.out.println("do some!"); } //没有static的方法 public void doOther() { System.out.println("do other!"); } //带有static的方法 public static void method1() { //调用doSOme(带有static的方法) //完整方法的调用 Test.doSome(); //省略方法的调用 doSome(); //调用doOther(没有static的方法) //完整方法的调用 Test t = new Test(); t.doOther(); //省略方法的调用 //无 //访问i(实例参数) //完整方法的调用 System.out.println(t.i); //省略方法的调用 //无 } //没有static的方法 public void method2() { //调用doSOme(带有static的方法) //完整方法的调用 Test.doSome(); //省略方法的调用 doSome();//this.doSome; //调用doOther(没有static的方法) //完整方法的调用 this.doOther();//调用的doOther中无static,且当前方法中也无static,当前方法有this,所以直接引用.即this.doOther() //省略方法的调用 doOther(); //访问i(实例参数) //完整方法的调用 System.out.println(this.i); //省略方法的调用 System.out.println(i); } //主方法 public static void main(String[] args) { //要求在这里编写程序调用method1(带有static的方法) //完整方法的调用 Test.method1(); //省略方法的调用 method1(); //要求在这里编写程序调用method2(没有static的方法) //完整方法的调用 Test t = new Test(); t.method2(); //省略方法的调用 //无 }}
复制代码
点击并拖拽以移动
\
评论