java Circle 类中有属性 radius,Cylinder 类中有属性 length,用继承编写两个圆的面积,体积,输出两个圆的 radius,color,weight,length 以及两个
下面是使用继承编写的 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
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
```
上一篇:Windows域管理的使用背景