设计Shape表示图形类,为抽象类,有面积属性area、周长属性per、颜色属性color,有两个构造方法(一个是默认的、一个是为颜色赋值的),还有3个抽象方法,分别是:getArea计算面积、getPer()计算周长、showAll输出所有信息,还有一个求颜色的方法getColor。接口Movaable,包括一个抽象方法:move(),输出图形移动信息。设计2个子类继承Shape,实现Moveable接口:Rectangle表示矩形类,增加两个属性,Width表示长度、Height表示宽度,重写getPer、getArea\showAll(显示矩形长、宽、颜色、面积、周长)和move()四个方法,另外又增加一个构造方法(一个是默认的、一个为高度、宽度、颜色赋值的)。Cirlcle表示圆类,增加1个属性,radius表示半径,重写getPer、getArea\showAll(显示矩形长、宽、颜色、面积、周长)和move()四个方法,另外又增加两个构造方法(一个是默认的、一个为半径、颜色赋值的)。测试类中,在main方法中,声明创建每个子类,并调用2个子类的showAll方法和move()方法。
时间: 2023-06-11 16:07:45 浏览: 248
设计一个抽象类图形类,在该类中包含有至少两个抽象方法求周长和求面积,分别定义圆形类、长方形类、正方形类、三角形类来继承图形类,并实现上述两个方法
5星 · 资源好评率100%
抽象类Shape的代码实现如下:
```java
public abstract class Shape {
protected double area;
protected double per;
protected String color;
public Shape() {
color = "white";
}
public Shape(String color) {
this.color = color;
}
public abstract double getArea();
public abstract double getPer();
public abstract void showAll();
public String getColor() {
return color;
}
public abstract void move();
}
```
接口Movable的代码实现如下:
```java
public interface Movable {
void move();
}
```
Rectangle类的代码实现如下:
```java
public class Rectangle extends Shape implements Movable {
private double width;
private double height;
public Rectangle() {
super();
width = 0;
height = 0;
}
public Rectangle(double width, double height, String color) {
super(color);
this.width = width;
this.height = height;
}
@Override
public double getArea() {
return width * height;
}
@Override
public double getPer() {
return 2 * (width + height);
}
@Override
public void showAll() {
System.out.println("Width: " + width);
System.out.println("Height: " + height);
System.out.println("Color: " + color);
System.out.println("Area: " + getArea());
System.out.println("Perimeter: " + getPer());
}
@Override
public void move() {
System.out.println("Rectangle is moving.");
}
}
```
Circle类的代码实现如下:
```java
public class Circle extends Shape implements Movable {
private double radius;
public Circle() {
super();
radius = 0;
}
public Circle(double radius, String color) {
super(color);
this.radius = radius;
}
@Override
public double getArea() {
return Math.PI * radius * radius;
}
@Override
public double getPer() {
return 2 * Math.PI * radius;
}
@Override
public void showAll() {
System.out.println("Radius: " + radius);
System.out.println("Color: " + color);
System.out.println("Area: " + getArea());
System.out.println("Perimeter: " + getPer());
}
@Override
public void move() {
System.out.println("Circle is moving.");
}
}
```
测试类的代码实现如下:
```java
public class Test {
public static void main(String[] args) {
Shape shape1 = new Rectangle();
Shape shape2 = new Circle();
Shape shape3 = new Rectangle(2, 3, "red");
Shape shape4 = new Circle(4, "blue");
shape1.showAll();
shape1.move();
shape2.showAll();
shape2.move();
shape3.showAll();
shape3.move();
shape4.showAll();
shape4.move();
}
}
```
输出结果如下:
```
Width: 0.0
Height: 0.0
Color: white
Area: 0.0
Perimeter: 0.0
Rectangle is moving.
Radius: 0.0
Color: white
Area: 0.0
Perimeter: 0.0
Circle is moving.
Width: 2.0
Height: 3.0
Color: red
Area: 6.0
Perimeter: 10.0
Rectangle is moving.
Radius: 4.0
Color: blue
Area: 50.26548245743669
Perimeter: 25.132741228718345
Circle is moving.
```
阅读全文