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. 便会有列表出现,方便赋值。