java继承与多态 类的继承

根据下图实现类。在TestCylinder类中创建Cylinder类的对象,设置圆柱的底面半径和高,并输出圆柱的体积。注:pi取值3.14

img


请大家踊跃发言,只采纳源代码,本问题只用于学习交流!

类的继承
Circle.java:

public class Circle {
    protected double radius;
    public Circle(){
        this.radius = 1;
    }
    public void setRadius(double radius){
        this.radius = radius;
    }
    public double getRadius(){
        return this.radius;
    }
    public double findArea(){
        return 3.14 * radius * radius;
    }

}


Cylinder.java


public class Cylinder extends Circle{
    private double length;
    public Cylinder(){
        length = 1;
    }
    public void setLength(double length){
        this.length = length;
    }
    public double getLength(){
        return this.length;
    }
    public double findVolume(){
        return 3.14 * radius * radius * length;
    }

}


TestCylinder.java

public class TestCylinder {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Cylinder c = new Cylinder();
        System.out.println("Cylinder volume:"+c.findVolume());

    }

}