java编写一个图形类Shape对数学中的图形进行抽象描述,并具有抽象方法可以计算图形的周长和面积(computCircum0和ComputArea0),具体分为以下三个子任务完成。 (1)创建Rectangle类继承于Shape类,添加属性width、height,并定义构造方法Rectangleldouble Width,double _height); (2)编程利用Rectangle对象输出一个30*40的矩形 30 的周长和面积。
时间: 2024-03-06 07:51:51 浏览: 88
好的,以下是代码实现:
```java
// Shape.java
public abstract class Shape {
public abstract double computCircum();
public abstract double computArea();
}
// 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;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
@Override
public double computCircum() {
return 2 * (width + height);
}
@Override
public double computArea() {
return width * height;
}
}
// Main.java
public class Main {
public static void main(String[] args) {
Rectangle rectangle = new Rectangle(30, 40);
System.out.println("矩形的周长为:" + rectangle.computCircum());
System.out.println("矩形的面积为:" + rectangle.computArea());
}
}
```
输出结果为:
```
矩形的周长为:140.0
矩形的面积为:1200.0
```
阅读全文