抽象类area类中只包含一个抽象方法double area子类round airlift通过覆盖父类中的抽象方法era求圆的面积,另一个子类era类通过覆盖父类中的抽象方法aa求长方形的面积,圆的半径和长方形的边分别定义为此类round area类和recon与都为类型,area类中接收键盘输入输入内容分别为长方形的边和圆的半径,并求出长方形和圆的面积显示在屏幕上。
时间: 2023-12-03 09:44:36 浏览: 18
以下是代码实现:
```java
import java.util.Scanner;
abstract class Area {
public abstract double area();
}
class Round extends Area {
private double radius;
public Round(double radius) {
this.radius = radius;
}
@Override
public double area() {
return Math.PI * radius * radius;
}
}
class Rectangle extends Area {
private double length;
private double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
@Override
public double area() {
return length * width;
}
}
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入圆的半径:");
double radius = input.nextDouble();
Round round = new Round(radius);
System.out.println("圆的面积为:" + round.area());
System.out.print("请输入长方形的长度:");
double length = input.nextDouble();
System.out.print("请输入长方形的宽度:");
double width = input.nextDouble();
Rectangle rectangle = new Rectangle(length, width);
System.out.println("长方形的面积为:" + rectangle.area());
}
}
```
程序运行后,会先输入圆的半径,然后计算并输出圆的面积;接着输入长方形的长度和宽度,然后计算并输出长方形的面积。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)