看代码说话,先来个简单的。
public
class
Wealthy
... {
public String answer = "Yes!";
public void wantMoney()
...{
String answer = "No!";
System.out.println("Do you want to give me $1,000,000? > " +
answer);
System.out.println("Would you like $1,000,000? > " +
this.answer);
}
public static void main(String[] args)
...{
Wealthy w = new Wealthy();
w.wantMoney();
}
}
运行结果:
Do you want to give me $ 1 , 000 , 000 ? > No !
Would you like $ 1 , 000 , 000 ? > Yes !
... {
public String answer = "Yes!";
public void wantMoney()
...{
String answer = "No!";
System.out.println("Do you want to give me $1,000,000? > " +
answer);
System.out.println("Would you like $1,000,000? > " +
this.answer);
}
public static void main(String[] args)
...{
Wealthy w = new Wealthy();
w.wantMoney();
}
}
运行结果:
Do you want to give me $ 1 , 000 , 000 ? > No !
Would you like $ 1 , 000 , 000 ? > Yes !
再来个复杂点的。
public
class
ThisPoint
...
{
public static void main(String[] args) ...{
A b = new B();
b.fb();
}
}
class A ... {
public A() ...{
}
public void fa() ...{
System.out.println("CLASS A :Function fa Runing......");
}
public void fb() ...{
System.out.println("CLASS A :Function fb Runing......");
fa();
System.out.println("CLASS A :Function fb Stop......");
}
}
class B extends A ... {
public B() ...{
}
public void fa() ...{
System.out.println("CLASS B :Function fa Runing......");
}
public void fb() ...{
System.out.println("CLASS B :Function fb Runing......");
super.fb();
System.out.println("CLASS B :Function fb Stop......");
}
}
运行结果:
CLASS B :Function fb Runing......
CLASS A :Function fb Runing......
CLASS B :Function fa Runing......
CLASS A :Function fb Stop......
CLASS B :Function fb Stop......
public static void main(String[] args) ...{
A b = new B();
b.fb();
}
}
class A ... {
public A() ...{
}
public void fa() ...{
System.out.println("CLASS A :Function fa Runing......");
}
public void fb() ...{
System.out.println("CLASS A :Function fb Runing......");
fa();
System.out.println("CLASS A :Function fb Stop......");
}
}
class B extends A ... {
public B() ...{
}
public void fa() ...{
System.out.println("CLASS B :Function fa Runing......");
}
public void fb() ...{
System.out.println("CLASS B :Function fb Runing......");
super.fb();
System.out.println("CLASS B :Function fb Stop......");
}
}
运行结果:
CLASS B :Function fb Runing......
CLASS A :Function fb Runing......
CLASS B :Function fa Runing......
CLASS A :Function fb Stop......
CLASS B :Function fb Stop......
能看出运行结果说明对this的理解就可以了。