抽象类Shape2D以及Shape3D,继承抽象父类Shape; 普通类Circle类和Rectangle类,继承Shape2D; 普通类Box类和Sphere类,继承Shape3D。 要求如下: (1)Shape类:抽象方法setColor(String color)方法,用来给图形设置颜色。 抽象方法showShapeInfo()方法,用来显示图形的详细信息。 (2)Shape2D类:计算面积的方法area(),计算周长的方法perimeter()。 (3)Shape3D类:计算表面积方法surfaceArea(),计算体积方法volume()。 (4)分别创建Rectangle、Circle、Box、Sphere类。 (5)在测试类TestShape中 创建Rectangle、Circle、Box、Sphere类对象; 对长方形、圆形;立方体、球体各对象的属性进行初始化; 输出各对象面积、周长、表面积、体积等详细信息。
时间: 2023-06-13 09:04:17 浏览: 116
class_shape.zip_class Shape_shape类继承
以下是代码实现:
```
// Shape.java
public abstract class Shape {
protected String color;
public abstract void setColor(String color);
public abstract void showShapeInfo();
}
// Shape2D.java
public abstract class Shape2D extends Shape {
public abstract double area();
public abstract double perimeter();
}
// Shape3D.java
public abstract class Shape3D extends Shape {
public abstract double surfaceArea();
public abstract double volume();
}
// Circle.java
public class Circle extends Shape2D {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public void setColor(String color) {
this.color = color;
}
@Override
public void showShapeInfo() {
System.out.println("Shape: Circle");
System.out.println("Radius: " + radius);
System.out.println("Color: " + color);
System.out.println("Area: " + area());
System.out.println("Perimeter: " + perimeter());
}
@Override
public double area() {
return Math.PI * radius * radius;
}
@Override
public double perimeter() {
return 2 * Math.PI * radius;
}
}
// Rectangle.java
public class Rectangle extends Shape2D {
private double width;
private double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
@Override
public void setColor(String color) {
this.color = color;
}
@Override
public void showShapeInfo() {
System.out.println("Shape: Rectangle");
System.out.println("Width: " + width);
System.out.println("Height: " + height);
System.out.println("Color: " + color);
System.out.println("Area: " + area());
System.out.println("Perimeter: " + perimeter());
}
@Override
public double area() {
return width * height;
}
@Override
public double perimeter() {
return 2 * (width + height);
}
}
// Box.java
public class Box extends Shape3D {
private double length;
private double width;
private double height;
public Box(double length, double width, double height) {
this.length = length;
this.width = width;
this.height = height;
}
@Override
public void setColor(String color) {
this.color = color;
}
@Override
public void showShapeInfo() {
System.out.println("Shape: Box");
System.out.println("Length: " + length);
System.out.println("Width: " + width);
System.out.println("Height: " + height);
System.out.println("Color: " + color);
System.out.println("Surface Area: " + surfaceArea());
System.out.println("Volume: " + volume());
}
@Override
public double surfaceArea() {
return 2 * (length * width + width * height + height * length);
}
@Override
public double volume() {
return length * width * height;
}
}
// Sphere.java
public class Sphere extends Shape3D {
private double radius;
public Sphere(double radius) {
this.radius = radius;
}
@Override
public void setColor(String color) {
this.color = color;
}
@Override
public void showShapeInfo() {
System.out.println("Shape: Sphere");
System.out.println("Radius: " + radius);
System.out.println("Color: " + color);
System.out.println("Surface Area: " + surfaceArea());
System.out.println("Volume: " + volume());
}
@Override
public double surfaceArea() {
return 4 * Math.PI * radius * radius;
}
@Override
public double volume() {
return 4 * Math.PI * radius * radius * radius / 3;
}
}
// TestShape.java
public class TestShape {
public static void main(String[] args) {
Circle circle = new Circle(5);
circle.setColor("Red");
circle.showShapeInfo();
System.out.println();
Rectangle rectangle = new Rectangle(3, 4);
rectangle.setColor("Blue");
rectangle.showShapeInfo();
System.out.println();
Box box = new Box(2, 3, 4);
box.setColor("Green");
box.showShapeInfo();
System.out.println();
Sphere sphere = new Sphere(5);
sphere.setColor("Yellow");
sphere.showShapeInfo();
}
}
```
输出结果为:
```
Shape: Circle
Radius: 5.0
Color: Red
Area: 78.53981633974483
Perimeter: 31.41592653589793
Shape: Rectangle
Width: 3.0
Height: 4.0
Color: Blue
Area: 12.0
Perimeter: 14.0
Shape: Box
Length: 2.0
Width: 3.0
Height: 4.0
Color: Green
Surface Area: 52.0
Volume: 24.0
Shape: Sphere
Radius: 5.0
Color: Yellow
Surface Area: 314.1592653589793
Volume: 523.5987755982989
```
阅读全文