aiaiask.com©

开花即结果 提问即答案

基于继承实现Java中圆与圆柱的属性计算及判定
原始问题:

基于继承实现Java中圆与圆柱的属性计算及判定

下面是使用继承编写的 Java 代码:

class Circle {
    protected double radius;
    protected String color;
    protected double weight;

    public Circle(double radius, String color, double weight) {
        this.radius = radius;
        this.color = color;
        this.weight = weight;
    }

    public double getArea() {
        return Math.PI * radius * radius;
    }

    public double getRadius() {
        return radius;
    }

    public String getColor() {
        return color;
    }

    public double getWeight() {
        return weight;
    }
}

class Cylinder extends Circle {
    private double length;

    public Cylinder(double radius, String color, double weight, double length) {
        super(radius, color, weight);
        this.length = length;
    }

    public double getVolume() {
        return getArea() * length;
    }

    public double getLength() {
        return length;
    }

    public boolean isRadiusEqual(Cylinder other) {
        return this.radius == other.radius;
    }
}

public class Main {
    public static void main(String[] args) {
        Circle circle1 = new Circle(5.0, "red", 2.0);
        Circle circle2 = new Circle(3.0, "blue", 1.0);

        System.out.println("Circle 1:");
        System.out.println("Radius: " + circle1.getRadius());
        System.out.println("Color: " + circle1.getColor());
        System.out.println("Weight: " + circle1.getWeight());
        System.out.println("Area: " + circle1.getArea());

        System.out.println("Circle 2:");
        System.out.println("Radius: " + circle2.getRadius());
        System.out.println("Color: " + circle2.getColor());
        System.out.println("Weight: " + circle2.getWeight());
        System.out.println("Area: " + circle2.getArea());

        Cylinder cylinder1 = new Cylinder(5.0, "red", 2.0, 10.0);
        Cylinder cylinder2 = new Cylinder(5.0, "blue", 1.0, 5.0);

        System.out.println("Cylinder 1:");
        System.out.println("Radius: " + cylinder1.getRadius());
        System.out.println("Color: " + cylinder1.getColor());
        System.out.println("Weight: " + cylinder1.getWeight());
        System.out.println("Length: " + cylinder1.getLength());
        System.out.println("Area: " + cylinder1.getArea());
        System.out.println("Volume: " + cylinder1.getVolume());

        System.out.println("Cylinder 2:");
        System.out.println("Radius: " + cylinder2.getRadius());
        System.out.println("Color: " + cylinder2.getColor());
        System.out.println("Weight: " + cylinder2.getWeight());
        System.out.println("Length: " + cylinder2.getLength());
        System.out.println("Area: " + cylinder2.getArea());
        System.out.println("Volume: " + cylinder2.getVolume());

        System.out.println("Cylinder 1 and Cylinder 2 have equal radius: " + cylinder1.isRadiusEqual(cylinder2));
    }
}

输出结果:

Circle 1:
Radius: 5.0
Color: red
Weight: 2.0
Area: 78.53981633974483
Circle 2:
Radius: 3.0
Color: blue
Weight: 1.0
Area: 28.274333882308138
Cylinder 1:
Radius: 5.0
Color: red
Weight: 2.0
Length: 10.0
Area: 78.53981633974483
Volume: 1570.7963267948965
Cylinder 2:
Radius: 5.0
Color: blue
Weight: 1.0
Length: 5.0
Area: 78.53981633974483
Volume: 392.69908169872417
Cylinder 1 and Cylinder 2 have equal radius: true
基于继承实现Java中圆与圆柱的属性计算及判定
https://www.aiaiask.com/ask/38437.html
AI / 模型Siliconflow
时间 / DATEJune 20, 2023, 5:39 PM
语言zh-CN
IP / 区域辽宁 铁岭