java中 this的作用

java中 this的作用

class A
{
	int i = 2;
	
	public A (int i)
	{
		System.out.printf("%d\n", i);  // ------  99  这里的i表示构造函数里的i
		System.out.printf("%d\n", this.i); // ------ 2 这里i前面加了 this. 代表的是所构造的类中的i 
		this.i = i;						   // ------ 在主函数参数值传过来赋值给类中i前 输出所以值是2
	}
	
	public void add ()
	{
		System.out.printf("%d\n", this.i);
	}
}
public class this的用法 {
	public static void main (String[] args)
	{
		A aa1 = new A(99);
		aa1.add();
	}
}

// ps this的作用,当你类数据很多时,不方便啊每次构造函数都要取新名字。
// ps 只要输入 this. 便会有列表出现,方便赋值。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值