当前位置: 技术问答>java相关
关于this的特殊用法
来源: 互联网 发布时间:2015-03-17
本文导语: 下面这个程序使用cast this来访问非直接的父类成员,也涉及到了方法的动态绑定。一般的动态绑定,我是知道的。 我就是看不太明白带星号注释的几行,请高手解释一下 public class ShadowTest { public static void main(...
下面这个程序使用cast this来访问非直接的父类成员,也涉及到了方法的动态绑定。一般的动态绑定,我是知道的。
我就是看不太明白带星号注释的几行,请高手解释一下
public class ShadowTest
{
public static void main(String s[])
{
new STChild().demo();
}
}
class STGrandParent
{
double wealth = 50000.00;
public double getWealth()
{
System.out.println("GrandParent-" + wealth);
return wealth;
}
}
class STParent extends STGrandParent
{
double wealth = 100000.00;
public double getWealth()
{
System.out.println("Parent-" + wealth);
return wealth;
}
}
class STChild extends STParent
{
double wealth = 200000.00;
public double getWealth()
{
System.out.println("Child-" + wealth);
return wealth;
}
public void demo()
{
getWealth(); // Calls Child method
super.getWealth(); // Calls Parent method
//super.super.getWealth(); // Compiler error, GrandParent method cannot be accessed
((STParent)this).getWealth(); // *****Calls Child method, due to dynamic method lookup
((STGrandParent)this).getWealth(); // *****Calls Child method, due to dynamic method lookup
System.out.println(wealth); // Prints Child wealth
System.out.println(super.wealth); // Prints Parent wealth
System.out.println(((STParent)this).wealth); // Prints Parent wealth
System.out.println(((STGrandParent)this).wealth); // Prints GrandParent wealth
}
}
我就是看不太明白带星号注释的几行,请高手解释一下
public class ShadowTest
{
public static void main(String s[])
{
new STChild().demo();
}
}
class STGrandParent
{
double wealth = 50000.00;
public double getWealth()
{
System.out.println("GrandParent-" + wealth);
return wealth;
}
}
class STParent extends STGrandParent
{
double wealth = 100000.00;
public double getWealth()
{
System.out.println("Parent-" + wealth);
return wealth;
}
}
class STChild extends STParent
{
double wealth = 200000.00;
public double getWealth()
{
System.out.println("Child-" + wealth);
return wealth;
}
public void demo()
{
getWealth(); // Calls Child method
super.getWealth(); // Calls Parent method
//super.super.getWealth(); // Compiler error, GrandParent method cannot be accessed
((STParent)this).getWealth(); // *****Calls Child method, due to dynamic method lookup
((STGrandParent)this).getWealth(); // *****Calls Child method, due to dynamic method lookup
System.out.println(wealth); // Prints Child wealth
System.out.println(super.wealth); // Prints Parent wealth
System.out.println(((STParent)this).wealth); // Prints Parent wealth
System.out.println(((STGrandParent)this).wealth); // Prints GrandParent wealth
}
}
|
this就是一个STChild 这样写就比较清楚了:
STChild x=this;
((STParent)x).getWealth();
这时应该是STChild的getWealth()被调用(override)
((STGrandParent)this).getWealth();
这时仍然应该是STChild的getWealth()被调用(override)
对于field,是“hide",所以
System.out.println(((STParent)this).wealth); 是10000.0
wealthSystem.out.println(((STGrandParent)this).wealth); 是5000.0
super指向”直接父类“,所以
System.out.println(super.wealth); 是10000.0
STChild x=this;
((STParent)x).getWealth();
这时应该是STChild的getWealth()被调用(override)
((STGrandParent)this).getWealth();
这时仍然应该是STChild的getWealth()被调用(override)
对于field,是“hide",所以
System.out.println(((STParent)this).wealth); 是10000.0
wealthSystem.out.println(((STGrandParent)this).wealth); 是5000.0
super指向”直接父类“,所以
System.out.println(super.wealth); 是10000.0