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);
}
}