我在Circle3.java中定义了radius这么一个值,但是为什么在Cylinder3.java中不加定义,也不用超类引用,就可以直接用它.
package c.czg;
import c.czg.Point3;
public class Circle3 extends Point3
{
protected double radius;
public Circle3()
{
setRadius(0.0);
}
public Circle3( double circleRadius,int xCoordinate,int yCoordinate )
{
super( xCoordinate,yCoordinate );
setRadius( circleRadius );
}
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;
}
public String toString()
{
return "Center = " + super.toString() +
"; Radius = " + radius;
}
public String getName()
{
return "Circle:";
}
}
package c.czg;
import c.czg.Circle3;
public class Cylinder3 extends Circle3
{
protected double height;
public Cylinder3()
{
setHeight(0);
}
public Cylinder3( double cylinderHeight,double cylinderRadius,int xCoordinate,int yCoordinate)
{
super( cylinderRadius,xCoordinate,yCoordinate );
setHeight( cylinderHeight );
}
public void setHeight( double cylinderHeight )
{
height = ( cylinderHeight >= 0.0 ? cylinderHeight : 0.0 );
}
public double getHeight()
{
return height;
}
public double area()
{
return 2 * super.area() + 2 * Math.PI * radius * height;
}
public double volume()
{
return super.area() * height;
}
public String toString()
{
return super.toString() + "; height = " + height;
}
public String getName()
{
return "Cylinder:";
}
}