郝斌Java--19(This)

TextThis1.java

class A
{
	public int i;
	
	public A(int j)
	{
		i = j;
	}
	
	public void show()
	{
		System.out.println("i = " + i);
	} 
}

class TestThis1
{
	public static void main(String[] args)
	{
		A aa1 = new A(5);
		A aa2 = new A(8);
		aa1.show();
		aa2.show();
	}
}
class A
{
	private int i;
	public A(int i)
	{
		this.i = i;  //将形参 i 赋给该构造方法本次运行所创建的那个新对象的i数据成员
	}
	public void show(){
		System.out.println("i = " + this.i);
			//this表示当前时刻正在调用show方法的对象
			//this可以省略
	}
}

public class TestThis
{
	public static void main(String[] args){
		A aa1 = new A(100);
		aa1.show();
		
		A aa2 = new A(200);
		aa2.show();
	}	
}

 

 

class A
{
	public int i;
	
	public A(int j)
	{
		i = j;
	}

	public void show()  //this代表的是当前正在调用show方法的对象
	{
		System.out.printf("i = %d\n", i); //i可以换成this.i
	}
}

public class TestThis_1
{
	public static void main(String[] args)
	{
		A aa1 = new A(10);
		A aa2 = new A(20);
		aa1.show();// aa1.show(aa1);
		aa2.show();// aa2.show(aa2);
	}
}

 

 

 

 

 

 

 

 

 

 

 

 

class A
{
	public int i = 99;
	
	public A(int i)
	{
		this.i = i; //this 代表当前时刻正在创建的对象
		System.out.printf("%d\n", i);
	}
	
	public void show()
	{
		System.out.printf("%d\n", this.i);  //this 代表正在调用show方法的对象
	}
}

public class TestThis_2
{
	public static void main(String[] args)
	{
		A aa = new A(2);
		aa.show();
		//System.out.printf("%d\n", aa.i);	
	}	
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值