this关键字的作用

作用:
1、调用本类中的方法
2、表示类中的属性
3、可以使用this调用本类的构造方法
4、this表示当前对象

1、调用本类中的方法

public class ThisDemo {

	public static void main(String[] args) {
		DemoThis d=new DemoThis(20);
		d.tellThis();
	}
}
class DemoThis{
	private int age;
	
	public DemoThis(int a) {
		age=a;
	}
	
	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
	
	public void tellThis() {
		System.out.println("年龄为:"+this.getAge());
	}
}
//结果:年龄为:20

使用“this.方法名称()”格式可以调用本类中的方法

2、表示类中的属性

class DemoThis{
	private int age;
	public DemoThis(int age) {
		this.age=age;
	}
	//等价于
	/*public DemoThis(int a) {
		age=a;
	}*/
	..........
}

如果构造方法中这样写

public DemoThis(int age) {
		age=age;
	}

其中“age=age”都是构造方法的参数——可以通过“就近原则”分析原理。

3、可以使用this调用本类的构造方法

//此程序中通过this()调用的某类的无参构造方法。

public class ThisDemo {
	public static void main(String[] args) {
		//构造方法在实例化对象时自动被调用
		//即,在类的所以方法中构造方法是被优先调用
		DemoThis d=new DemoThis(20);
	}
}

class DemoThis{
	private int age;
	public DemoThis(int age) {
		this();    //调用无参构造方法
		this.age=age;
	}
	public DemoThis() {
		System.out.println("此无参构造方法会被this()调用");
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}

this调用构造方法必须也只能放在构造方法的首行
this调用构造方法时一定要留一个构造方法作为出口,即程序中至少有一个构造方法未使用this调用其他构造方法——一般将无参构造方法作为出口。

4、this表示当前对象(this最重要的特点)
Java当前对象:指当前正在调用类中方法的对象。
解释:
假设现在有张三、李四两人,如果现在在吃饭的人是张三,则“当前正在吃饭的人”是张三。
比较对象是否相等步骤
1、首先比较对象地址是否相等
2、若地址不相等,比较属性。

public class ThisDemo02 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		PersonThis p1=new PersonThis("张三",16);
		PersonThis p2=new PersonThis("张三",16);
		if(p1.compare(p2)) {
			System.out.println("两对象的在地址和属性上全相同");
		}else {
			System.out.println("两对象不完全相同");
		}
	}

}
class PersonThis{
	private String name;
	private int age;
	
	public PersonThis(String name,int age) {
		this.name=name;
		this.age=age;
	}
	
	public boolean compare(PersonThis per) {
		PersonThis p1=this;	//表示当前调用方法的对象——此时谁调用compare(),this就表示谁
		PersonThis p2=per;	//传递到方法的对象
		//首先堆地址进行判断
		if(p1==p2) {	
			return true;
		}
		//对对象的属性进行判断
		if(p1.name.equals(p2.name)&&p1.age==p2.age) {
			return true;
		}else {
			return false;
		}
	}
	
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
}

在程序中可能存在多个对象,此时通过this就能表示当前对象
假设对象A调用了类中的方法function(),那么这个function()中的 this就为当前执行的对象A。

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
this关键字在Java中有多种作用。首先,它可以用于任何实例方法内,指向当前对象。这意味着我们可以使用this来引用当前对象的属性或方法。 其次,this关键字还可以在需要引用调用当前方法的对象时使用。这对于链式调用或在方法内部调用其他方法时非常有用。 此外,this关键字还可以用于在构造函数中引用当前对象。当创建新对象时,构造函数会被自动调用,我们可以使用this关键字来引用当前对象,并对对象的属性进行赋值操作。这在构造函数中使用参数与成员变量同名时非常有用。通过使用this关键字,我们可以将参数值正确地赋给成员变量,而不是重新给参数赋值。 总结来说,this关键字作用包括指向当前对象、引用调用当前方法的对象以及在构造函数中引用当前对象。这使得我们可以更方便地操作对象的属性和方法,并确保正确地初始化对象的成员变量。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Java this关键字详解](https://blog.csdn.net/weixin_45743799/article/details/104774391)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [this关键字详解](https://download.csdn.net/download/weixin_38686924/12750400)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [this关键字作用](https://blog.csdn.net/pt666/article/details/71298596)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值