qcode
  • 创作工场专业版,一问多答!更智能、更完整、更原创
  • 这是一个超强内容创作平台,它会写文章、做设计、绘图、敲代码,有问必答!
🔥点击使用-免登录深度版🔥

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

时间2023-06-20 17:39:04 提问来自 辽宁省铁岭市 的网友 分类创作 浏览255
下面是使用继承编写的 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 ```

不满意?免费深度重写!

免责声明:
以上内容除特别注明外均来源于网友提问,创作工场回答,未经许可,严谨转载。

上一篇 人才战略与激励政策 下一篇 发条橙 A Clockwork Orange影评