用java1.(1)设计一个形状类 Shape,包含一个 getArea()方法,该方法不包含实际语句。 (2)在 Shape 类基础上设计圆形、矩形、三角形和梯形四个子类,要求根据实际形状重写 getArea()方法。 (3)设计一个 TestShape 类,包含变量 area(存储总面积)、静态方法 countArea(Shape s),该方法负责把参数中的形状面积加入到 area 中。在 main 函数中新建(2)中四种类型的对象s1、s2、s3、s4,通过调用 countArea 方法把四个对象面
时间: 2024-03-17 21:44:29 浏览: 27
积加入到 area 中,并输出 area 的值。
代码如下:
Shape.java
```
public class Shape {
public double getArea() {
return 0;
}
}
```
Circle.java
```
public class Circle extends Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double getArea() {
return Math.PI * radius * radius;
}
}
```
Rectangle.java
```
public class Rectangle extends Shape {
private double width;
private double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
@Override
public double getArea() {
return width * height;
}
}
```
Triangle.java
```
public class Triangle extends Shape {
private double base;
private double height;
public Triangle(double base, double height) {
this.base = base;
this.height = height;
}
@Override
public double getArea() {
return 0.5 * base * height;
}
}
```
Trapezoid.java
```
public class Trapezoid extends Shape {
private double topWidth;
private double bottomWidth;
private double height;
public Trapezoid(double topWidth, double bottomWidth, double height) {
this.topWidth = topWidth;
this.bottomWidth = bottomWidth;
this.height = height;
}
@Override
public double getArea() {
return 0.5 * (topWidth + bottomWidth) * height;
}
}
```
TestShape.java
```
public class TestShape {
private static double area = 0;
public static void countArea(Shape s) {
area += s.getArea();
}
public static void main(String[] args) {
Circle s1 = new Circle(2);
Rectangle s2 = new Rectangle(2, 3);
Triangle s3 = new Triangle(2, 3);
Trapezoid s4 = new Trapezoid(2, 4, 3);
countArea(s1);
countArea(s2);
countArea(s3);
countArea(s4);
System.out.println("Total area: " + area);
}
}
```
输出结果为:
```
Total area: 14.84955592153876
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)