//Circle1.java
package song;
public class Circle1 extends Point1 {
private double radius;
public Circle1(){
//��ʽ�ص��ó���Point1���޲ι��캯��
System.out.println( "Circle1 no-argument constructor: " + this );
}
public Circle1( int xValue, int yValue, double radiusValue ){
super( xValue, yValue ); //��ʽ�ص��ó���Ĵ�}�����Ĺ��캯��
setRadius( radiusValue );
System.out.println( "Circle1 constructor: " + this );
}
protected void finalize(){
System.out.println( "Circle1 finalizer: " + this );
super.finalize(); //�����finalize����
}
public void setRadius( double radiusValue ){
radius = ( radiusValue < 0.0 ? 0.0 : radiusValue );
}
public double getRadius(){
return radius;
}
public double getDiameter(){
return 2 * getRadius();
}
public double getCircumference(){
return Math.PI * getDiameter();
}
public double getArea(){
return Math.PI * getRadius() * getRadius();
}
public String toString(){
return "Center = " + super.toString() + "; Radius = " + getRadius();
}
}
package song;
public class Circle1 extends Point1 {
private double radius;
public Circle1(){
//��ʽ�ص��ó���Point1���޲ι��캯��
System.out.println( "Circle1 no-argument constructor: " + this );
}
public Circle1( int xValue, int yValue, double radiusValue ){
super( xValue, yValue ); //��ʽ�ص��ó���Ĵ�}�����Ĺ��캯��
setRadius( radiusValue );
System.out.println( "Circle1 constructor: " + this );
}
protected void finalize(){
System.out.println( "Circle1 finalizer: " + this );
super.finalize(); //�����finalize����
}
public void setRadius( double radiusValue ){
radius = ( radiusValue < 0.0 ? 0.0 : radiusValue );
}
public double getRadius(){
return radius;
}
public double getDiameter(){
return 2 * getRadius();
}
public double getCircumference(){
return Math.PI * getDiameter();
}
public double getArea(){
return Math.PI * getRadius() * getRadius();
}
public String toString(){
return "Center = " + super.toString() + "; Radius = " + getRadius();
}
}
public class Point1 {
private int x;
private int y;
public Point1(){ //Point1类的无参构造函数
System.out.println( "Point1 no-argument constructor: " + this );
}
public Point1( int xValue, int yValue ){ //Point1类的带有参数的构造函数
System.out.println( "Point1 constructor: " + this );
}
protected void finalize(){ //Point1类的finalize方法
System.out.println( "Point1 finalizer: " + this );
}
public void setX( int xValue ){
x = xValue;
}
public int getX(){
return x;
}
public void setY( int yValue ){
y = yValue;
}
public int getY(){
return y;
}
public String toString(){
return "[" + getX() + ", " + getY() + "]";
}
}
public class ConstructorFinalizerTest {
public static void main( String args[] ){
Point1 point;
Circle1 circle1, circle2;
point = new Point1( 11, 22 ); //创建Point1类的对象将调用该类的构造函数
System.out.println();
//创建Circle1类的对象将调用该类的构造函数
//在调用Circle1类的构造函数前,首先要调用超类的构造函数
circle1 = new Circle1( 72, 29, 4.5 );
System.out.println();
//创建Circle1类的对象将调用该类的构造函数
//在调用Circle1类的构造函数前,首先要调用超类的构造函数
circle2 = new Circle1( 5, 7, 10.67 );
point = null; //将point的引用设置为null
circle1 = null; //将circle1的引用设置为null
circle2 = null; //将circle2的引用设置为null
System.out.println();
//显式调用垃圾收集程序将调用设为null的对象的finalize方法
System.gc();
}
}
Point1 constructor: [100, 100]Point1 constructor: Center = [100, 100]; Radius = 0.0
Circle1 constructor: Center = [100, 100]; Radius = 4.5Point1 constructor: Center = [100, 100]; Radius = 0.0 ????
Circle1 constructor: Center = [100, 100]; Radius = 10.67Point1 finalizer: [100, 100]
Circle1 finalizer: Center = [100, 100]; Radius = 4.5
Point1 finalizer: Center = [100, 100]; Radius = 4.5 ????
Circle1 finalizer: Center = [100, 100]; Radius = 10.67
Point1 finalizer: Center = [100, 100]; Radius = 10.67有????号的地方请高手解释一下原因,谢谢!
super( xValue, yValue ); 在调用public Point1( int xValue, int yValue ){ //Point1类的带有参数的构造函数
System.out.println( "Point1 constructor: " + this );
所以使这个结果
第二个是
执行 circle1 = null; 调用
protected void finalize(){
System.out.println( "Circle1 finalizer: " + this );
super.finalize(); //�����finalize����
}
在调用
protected void finalize(){ //Point1类的finalize方法
System.out.println( "Point1 finalizer: " + this );
}
okle码
Point1 constructor: Center = [100, 100]; Radius = 0.0 中的Radius = 0.0,是怎么得出来的,Point1 finalizer: Center = [100, 100]; Radius = 4.5 中的Radius = 4.5 是怎么得出来得,谢谢!
我还有一处不明白,根据你说的“根据第2条,这时实际上调用的呢,是子类的toString()。所以子类的toString()被调用,对应的语句是return "Center = " + super.toString() + "; Radius = " + getRadius();”而这句话中又调用的父类的tostring()方法,而调用父类的tostring方法实际上是调用子类的tostring方法,这样不是要形成循环调用了吗??!!请解答困惑,谢谢!