就近原则:this关键字的用法总结
1、调用this的流程
public class thisUse {
// 成员属性,年龄
int month;
public void run(int month){
month = month;
System.out.println("小猫已经" + month + "月大了");
}
public static void main(String[] args) {
thisUse thisUse = new thisUse();
int mh = 1;
thisUse.run(mh);
System.out.println("小猫已经" + thisUse.month + "月大了");
}
/**
* 输出结果:
* 小猫已经1月大了
* 小猫已经0月大了
* */
代码运行过程:
/**
- 1、main方法启动,执行第一条语句,产生thisUse类型对象thisUse,此时由于thisUse类中没有定义构造方法,系统默认提供无参构造,并设置其属性month的默认值为0。
- 2、创建一个int类型的变量mh,赋值为1。
- 3、调用one的run方法,将mh作为实参传入run。此时,run方法的参数month即为1。
- 4、run方法执行第一句,由于此时在run方法中,由于就近原则的影响,其可访问范围内的month,最近的是方法形参的month ,
- 也就是说month=month,执行操作实际为,方法形参的自身赋值,说白了,这句话的执行类似于1=1,无实际意义,
- 5、run方法执行第二句,将赋值结果后的month传入输出语句,打印输出。即输出结果的第一行:小猫已经1月大了,回到主方法,由于此时thisUse.month仍是初始值O,未被修改过,因此最后打印输出第二行:小猫已经0月大了,当实际输出结果需要为1和1时,常用修订方案:
*/
修改方案1:
只修改run方法,其余不变
public void run(int Amonth){
month = Amonth;
System.out.println("小猫已经" + Amonth + "月大了");
}
/**
* 输出结果:
* 小猫已经1月大了
* 小猫已经1月大了
* */
修改run方法参数名,使其与类中属性名不同,此时再执行run方法第一句时,其可访问范围内"的month就需要向上找类中的成员属性month了,也就会对成员属性赋值,从而影响主方法中属性输出结果。
修改方案2:
仍只修改run方法,其余不变
public void run(int month){
this.month = month;
System.out.println("小猫已经" + month + "月大了");
}
/**
* 输出结果:
* 小猫已经1月大了
* 小猫已经1月大了
* */
修改方法中的第一行代码,通过this关键字,强行指代类中的成员属性month接收方法参数的赋值。此时由于this的影响。run方法传入的参数,将会直接给属性赋值,进而影响主方法最后的输出结果。
2、this的使用:调用属性和方法
package com.imooc.thisUse;
/**
* @version 1.0
* @Author 旺
* @Date 2021/1/8 20:03
* @注释:
*/
public class thisUse1 {
// 年龄、昵称
int month;
String name;
public void run(int month){
this.month = month;
System.out.println("小猫已经" + month + "月大了");
}
public void eat(){
this.name = "花花";
System.out.println("名叫" + this.name + "的小猫在吃鱼");
}
/**
* 去掉this:
* 结论:大部分时候,类的方法中访问成员属性时无须使用this,但如果方法里存在局部变量和成员属性同名,
* 但程序又需要在该方法里访问成员属性时,则必须使用this 进行调用。
* @param month
*/
public void run1(int month){
month = month;
System.out.println("小猫已经" + month + "月大了");
}
public void eat1(){
name = "花花";
System.out.println("名叫" + this.name + "的小猫在吃鱼");
}
/**
* this调用。使用同一个类中的其它方法
* 结论:大部分时候,类的成员方法访问同类中其他成员方法时,this关键字可加可不加,效果相同。
*/
public void run3(int month){
this.eat();
eat();
}
public static void main(String[] args) {
thisUse1 thisUse1 = new thisUse1();
thisUse1.run(1);
System.out.println("小猫已经" + thisUse1.month + "月大了");
thisUse1.eat();
System.out.println("---------");
thisUse1 thisUse2 = new thisUse1();
thisUse2.run1(2);
System.out.println("小猫已经" + thisUse2.month + "月大了");
thisUse2.eat1();
System.out.println("---------");
thisUse1.run3(3);
}
}
/** 输出结果:
* 小猫已经1月大了
* 小猫已经1月大了
* 名叫花花的小猫在吃鱼
* ---------
* 小猫已经2月大了
* 小猫已经0月大了
* 名叫花花的小猫在吃鱼
* ---------
* 名叫花花的小猫在吃鱼
* 名叫花花的小猫在吃鱼
* */
3、this在方法调用的时候,作为参数传递
- 在方法调用的时候,作为参数传递,在thisUse2类中,定义run方法,方法参数为thisUse2类型对象。call方法中,调用run方法,并通过this指代,传入当前对象。
- 结论:this在调用的时候,this可以作为方法参数进行传递,代表调用方法的当前对象。
package com.imooc.thisUse;
public class thisUse2 {
String name;
public void call(){
run(this);
}
/**
* 上边的call相当于此方法,只是在调用的时候需要传参cat.call(cat);
*/
public void call(thisUse2 temp){
run(temp);
}
public void run(thisUse2 cat){
System.out.println("昵称为:" + cat.name + "的小猫在奔跑");
}
public static void main(String[] args) {
thisUse2 cat = new thisUse2();
cat.name = "花花";
cat.call();
System.out.println("----------");
cat.name = "画虎";
cat.call(cat);
}
}
/**
*运行结果
*
昵称为:花花的小猫在奔跑
----------
昵称为:画虎的小猫在奔跑
*/
4、在方法调用时,作为返回值传递
public class thisUse3 {
String name;
int n;
public thisUse3 call(String name, int n){
this.n = n;
this.name = name;
return this;
}
public static void main(String[] args) {
thisUse3 thisUse3= new thisUse3();
thisUse3 temp = thisUse3.call("花花", 1);
System.out.println(thisUse3.name + thisUse3.n);
}
}
/**
*运行结果
*花花1
*/
this可以作为方法返回值,代表返回当前调用对象。
训练一个题,答案:A、3 4 ;B、7;C、6;D、8
public class thisWork {
int x, y;
public thisWork(int x, int y){
this.x = x;
this.y = y;
}
public static void main(String[] args) {
thisWork thisWork1, thisWork2;
thisWork1 = new thisWork(3, 3);
thisWork2 = new thisWork(4, 4);
System.out.println(thisWork1.x + + thisWork2.y);
System.out.println(thisWork1.x + " " + thisWork2.y);
}
}