class Demo{
public static void main(
String[] args){
(new SSC()).demo();
}
}
class C{
double value=300.00;
public double getvalue(){
System.out.println("C.value="+value);
return value;
}
}
class SC extends C{
double value=400.00;
public double getvalue(){
System.out.println("SC.value="+value);
return value;
}
}
class SSC extends SC{
double value=500.00;
public double getvalue(){
System.out.println("SSC.value="+value);
return value;
}
public void demo(){
getvalue();
((SC)this).getvalue();
((C)this).getvalue();
System.out.println();
System.out.println(value);
System.out.println(((SC)this).value);
System.out.println(((C)this).value);
}
}
the result is:
SSC.value=500.00
SSC.value=500.00
SSC.value=500.00
500.00
400.00
300.00
The reason is: method binds to a object at runntime
and variable binds depend on the object type at compiletime;
But i think just because of this reason,the answer is
SSC.value=500.00
SC.value=400.00
C.value=300.00
500.00
500.00
500.00
.
So what is the "(SC)this" really refers to at compiletime and runtime?
Is the result means that "(SC)this" acts at compiletime?
[This message has been edited by theMatrix (edited June 18, 2001).]