Java继承与多态 类的继承

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

img


请大家发表一下自己的观点,本提问只供学习参考

横线上方-号是私有属性
横线下方+号是共有方法
然后创建标准类就行了呀

之前写过,差不多这样吧,看下哪里不符合你条件的 修修改改

public class Circle {
    public double radius=1;
    public double get_r(double r){
        return r; }
    public void set_r(double r) {
        this.radius= r; }
        public Circle(double radius){
        this.radius=radius;
        }
    public Circle(){
    }

    public double findArea() {
        return 3.14*radius*radius; }
}

class Cylinder extends Circle{
    private Circle bottom;
    private double length;

    public Cylinder(double radius) {
        super(radius);
    }
    public Cylinder( Circle bottom,double length) {
        this.bottom=bottom;
        this.length=length;
    }
    public Cylinder() {
        super();
    }

    public Circle getBottom() {
        return bottom;
    }

    public void setBottom(Circle bottom) {
        this.bottom = bottom;
    }

    public double getLength() {
        return length;
    }

    public void setLength(double length) {
        this.length = length;
    }


    public double findVolume() {
        return this.bottom.findArea()*length; }}

class CircleTest {
    public static void main(String[] args) {
        Circle circle=new Circle(5);
        Cylinder cylinder=new Cylinder(circle,5);
        System.out.println(cylinder.findVolume());
    }

}

踊跃发表意见🥰