program10_6.java
来自「《A first book of java》by Gary J.Bronson 」· Java 代码 · 共 42 行
JAVA
42 行
public class Circle
{
// class variable declaration section
public final static double PI = 2.0 * Math.asin(1.0);
protected double radius;
// class method definition section
public Circle(double r) // constructor
{
radius = r;
}
public double calcval() // this calculates an area
{
return(PI * radius * radius);
}
}
class Cylinder extends Circle // Cylinder is derived from Circle
{
// class variable declaration section
protected double length; // add an additional data member
// class method definition section
public Cylinder(double r, double l)
{
super(r);
length = l;
}
public double calcval() // this calculates a volume
{
return (length * super.calcval()); // note the base method call
}
public static void main(String[] args)
{
Circle circleOne = new Circle(1); // create two Circle objects
Circle circleTwo = new Circle(2);
Cylinder cylinderOne = new Cylinder(3,4); // create one Cylinder object
System.out.println("The area of circleOne is " + circleOne.calcval());
System.out.println("The area of circletwo is " + circleTwo.calcval());
System.out.println("The volume of cylinderOne is " + cylinderOne.calcval());
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?