当前位置: 代码迷 >> Java相关 >> [求助]我不能理解:为什么会先调用了Test1 中的circle1 后不是直接调用circl ...
  详细解决方案

[求助]我不能理解:为什么会先调用了Test1 中的circle1 后不是直接调用circl ...

热度:594   发布时间:2006-12-06 18:18:09.0
[求助]我不能理解:为什么会先调用了Test1 中的circle1 后不是直接调用circle2

我不能理解:为什么会先调用了Test1 中的circle1 后不是直接调用circle2 而是是去调用了后面的System中的circle1.area() 和 circle2.area(); 然后再调用circle2中的各项,另外:为什么在Point1的构造函数中的System.out.println此句显示不出来?

import c.czg.Circle1;

public class Test1
{
public static void main ( String args[] )
{
Circle1 circle1,circle2;

circle1 = new Circle1 ( 4.5,72,29 );
circle2 = new Circle1 ( 10,5,5 );

System.out.println ( "The Circle1's area is :" + circle1.area() );
System.out.println ( "The Circle2's area is :" + circle2.area() );

circle1 = null;
circle2 = null;

System.gc();
}
}



另外一个是:
package c.czg;

import c.czg.Point1;

public class Circle1 extends Point1
{
protected double radius;

public Circle1()
{
radius = 0;
System.out.println ( "Circle3 constructor:" + this );
}

public Circle1 ( double circleRadius,int xCoordinate,int yCoordinate )
{
super( xCoordinate,yCoordinate );
radius = circleRadius;
System.out.println ( "Circle4 constructor:" + this );
}

public void setRadius ( double circleRadius )
{
radius = ( circleRadius >= 0.00 ? circleRadius : 0.00 );
}

public double getRadius ()
{
return radius;
}

public double area()
{
return Math.PI * radius * radius;
}

protected void finalize()
{
System.out.println ( "Circle5 finalizer:" + this );
super.finalize();
}

public String toString()
{
return " Center = " + super.toString() +
"; Radius = " + radius;
}
}



package c.czg;

public class Point1 extends Object {
protected int x,y;

public Point1()
{
x = 0 ;
y = 0 ;
System.out.println( "point0 constructor:" + this );
}

public Point1 ( int xCoording ,int yCoording )
{
x = xCoording ;
y = yCoording ;
System.out.println ( "Point1 constructor:" + this );
}

protected void finalize()
{
System.out.println ( "Point2 finalizer: " + this );
}

public String toString()
{
return "[" + x + "," + y + "]" ;
}
}


----------------解决方案--------------------------------------------------------
看来楼主需要好好补下基础了。
“为什么会先调用了Test1 中的circle1 后不是直接调用circle2 而是是去调用了后面的System中的circle1.area() 和 circle2.area(); 然后再调用circle2中的各项”
这句话问得太奇怪了,当然是你想调用谁就调用谁了,这里的circle1,circle2只是个名字而已。。。
“为什么在Point1的构造函数中的System.out.println此句显示不出来?”
此句是哪句呀?希望下次问问题时候说清楚。我看你可能是没明白构造函数覆盖。

----------------解决方案--------------------------------------------------------
  恕我语文水平低,我也不是很明白什么意思
----------------解决方案--------------------------------------------------------
老兄啊,你程序很正常啊,没啥问题。我运行结果是:
Point1 constructor: Center = [72,29]; Radius = 0.0
Circle4 constructor: Center = [72,29]; Radius = 4.5
Point1 constructor: Center = [5,5]; Radius = 0.0
Circle4 constructor: Center = [5,5]; Radius = 10.0
The Circle1's area is :63.61725123519331
The Circle2's area is :314.1592653589793
Circle5 finalizer: Center = [5,5]; Radius = 10.0
Point2 finalizer: Center = [5,5]; Radius = 10.0
Circle5 finalizer: Center = [72,29]; Radius = 4.5
Point2 finalizer: Center = [72,29]; Radius = 4.5

这个结果不对么?



----------------解决方案--------------------------------------------------------
以下是引用林小羽在2006-12-7 12:42:10的发言:
老兄啊,你程序很正常啊,没啥问题。我运行结果是:
Point1 constructor: Center = [72,29]; Radius = 0.0
Circle4 constructor: Center = [72,29]; Radius = 4.5
Point1 constructor: Center = [5,5]; Radius = 0.0
Circle4 constructor: Center = [5,5]; Radius = 10.0
The Circle1's area is :63.61725123519331
The Circle2's area is :314.1592653589793
Circle5 finalizer: Center = [5,5]; Radius = 10.0
Point2 finalizer: Center = [5,5]; Radius = 10.0
Circle5 finalizer: Center = [72,29]; Radius = 4.5
Point2 finalizer: Center = [72,29]; Radius = 4.5

这个结果不对么?


看来你也没看懂他问什么,他是不知道为什么程序那么写,为什么结果是那样的。。。


----------------解决方案--------------------------------------------------------