设计两个Graphics类的子类:Rectangle类(矩形)和Circle类(圆),编译并运行使程序运行结果如下所示: 矩形的面积为: 100.0 圆的面积为: 314.1592653589793
时间: 2024-05-13 21:19:23 浏览: 11
下面是代码实现:
```java
// Graphics.java
public abstract class Graphics {
public abstract double area();
}
// Rectangle.java
public class Rectangle extends Graphics {
private double width;
private double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
public double area() {
return width * height;
}
}
// Circle.java
public class Circle extends Graphics {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double area() {
return Math.PI * radius * radius;
}
}
// Main.java
public class Main {
public static void main(String[] args) {
Rectangle rectangle = new Rectangle(10, 10);
System.out.println("矩形的面积为: " + rectangle.area());
Circle circle = new Circle(10);
System.out.println("圆的面积为: " + circle.area());
}
}
```
输出结果为:
```
矩形的面积为: 100.0
圆的面积为: 314.1592653589793
```
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![uml](https://img-home.csdnimg.cn/images/20210720083646.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)