定义一个圆类,类名Circle,在类Circle中定义数据成员r(圆的半径),定义成员方法getZhou( ),返回圆的

1个回答

  • 把这三个类分别放到三个.java文件中,然后编译运行即可.

    public class Circle{

    private double r;

    public Circle(double r){

    this.r = r;

    }

    public double getZhou(){

    return 2*Math.PI*r;

    }

    public double getArea(){

    return Math.PI*r*r;

    }

    public void setR(double r){

    this.r = r;

    }

    public int getR(){

    return r;

    }

    }

    public class Cylinder extends Circle{

    private double h;

    public Cylinder(double r, double h){

    super(r);

    this.h = h;

    }

    public double getFaceArea(){

    getZhou() * h;

    }

    public double getVolumn(){

    getArea() * h;

    }

    public void setH(double h){

    this.h = h;

    }

    public int getH(){

    return h;

    }

    }

    public class mainClass{

    public static void main(String[] args){

    Circle c = new Circle(2);

    Cylinder cy = new Cylinder(3,5);

    System.out.println("半径为"+c.getR()+"的圆"+"周长为"+c.getZhou()+",面积为"+c.getArea());

    System.out.println("半径为"+cr.getR()+",高为"+cr.getH()+"的圆柱"+"表面积为"+cr.getFaceArea()+",体积为"+cr.getVolumn());)

    }

    }