多态调用例题详解
package upcasting;
class point {
int x, y;
2. point ( int a, int b) { a= 1 , b= 1
x= a; y= b;
System. out. println ( "point constructor:" + getString ( ) ) ;
}
public void finalize ( ) {
System. out. println ( "point finalizer:" + getString ( ) ) ;
}
4. ( 7. ) String getString ( ) { return "x=" + x+ " y=" + y; }
}
class circle extends point {
int radius;
circle ( int r, int a, int b) {
1. super ( a, b) ; 5. radius= r;
6. System. out. println ( "circle constructor:" + getString ( ) ) ;
} 调用子类的getString ( ) 方法
public void finalize ( ) {
System. out. println ( "circle finalizer:" + getString ( ) ) ;
}
3. (6. )String getString ( ) {
return super . getString ( ) + " radius=" + radius;
}
}
public class downcasting {
public static void main ( String args[ ] ) {
point c1, c2;
c1= new circle ( 1 , 1 , 1 ) ;
c2= new circle ( 2 , 2 , 2 ) ;
c1= null;
c2= null;
System. gc ( ) ;
}
}
#
point constructor: x= 1 y= 1 radius= 0
circle constructor: x= 1 y= 1 radius= 1
point constructor: x= 2 y= 2 radius= 0
circle constructor: x= 2 y= 2 radius= 2
circle finalizer: x= 2 y= 2 radius= 2
circle finalizer: x= 1 y= 1 radius= 1
更详尽的步骤可以通过单步调试看的更加清楚!