(1)编写图形抽象类 Shape ,其包含一个常量 PI 、颜色属性、两个抽象方法 getArea ()和 getPerimeter ()。 (2) Circle 类继承了抽象类 Shape ,包含一个私有属性 radius ,实现抽象方法 getArea () Cricle [5]。 和 getPerimeter (),覆盖方法 toString ()实现自定义圆的输出信息,如半径为5的圆输出 (3) Triangle 类继承了抽象类 Shape ,包含3个私有属性 a 、 b 、 c ,实现抽象方法 getArea ()和输出 Triangle [3,4,5]。 getPerimeter (),覆盖方法 toString ()实现自定义三角形的输出信息,如边3、4、5的三角形 (4) Rectangle 类继承了抽象类 Shape ,包含两个私有属性 length 、 width ,实现抽象方法 getArea ()和 getPerimeter (),覆盖方法 toString ()实现自定义矩形的输出信息,如长4宽5的矩形输出 Rectangle [4.5]。 (5) ShapeTest 类是一个测试类,对于 public void addOne ( Shape shape ),如果传人的是 Circle 的对象,则半径加1;如果传人的是 Rectangle 的对象,则长和宽分别加1;如果传人的是 Triangle 的对象,则3条边分别加1。 (6)在 ShapeTest 类的 main 方法中要求构造 Circle 类、 Triangle 类、 Rectangle 类的对象,并对相关方法进行测试。
时间: 2023-05-18 22:06:00 浏览: 120
编写一个抽象类Shape
4星 · 用户满意度95%
以下是代码实现:
```java
// Shape.java
public abstract class Shape {
public static final double PI = 3.14159265358979323846;
private String color;
public Shape(String color) {
this.color = color;
}
public String getColor() {
return color;
}
public abstract double getArea();
public abstract double getPerimeter();
}
// Circle.java
public class Circle extends Shape {
private double radius;
public Circle(String color, double radius) {
super(color);
this.radius = radius;
}
public double getRadius() {
return radius;
}
@Override
public double getArea() {
return PI * radius * radius;
}
@Override
public double getPerimeter() {
return 2 * PI * radius;
}
@Override
public String toString() {
return "Circle [" + radius + "]";
}
}
// Triangle.java
public class Triangle extends Shape {
private double a;
private double b;
private double c;
public Triangle(String color, double a, double b, double c) {
super(color);
this.a = a;
this.b = b;
this.c = c;
}
public double getA() {
return a;
}
public double getB() {
return b;
}
public double getC() {
return c;
}
@Override
public double getArea() {
double p = (a + b + c) / 2;
return Math.sqrt(p * (p - a) * (p - b) * (p - c));
}
@Override
public double getPerimeter() {
return a + b + c;
}
@Override
public String toString() {
return "Triangle [" + a + "," + b + "," + c + "]";
}
}
// Rectangle.java
public class Rectangle extends Shape {
private double length;
private double width;
public Rectangle(String color, double length, double width) {
super(color);
this.length = length;
this.width = width;
}
public double getLength() {
return length;
}
public double getWidth() {
return width;
}
@Override
public double getArea() {
return length * width;
}
@Override
public double getPerimeter() {
return 2 * (length + width);
}
@Override
public String toString() {
return "Rectangle [" + length + "," + width + "]";
}
}
// ShapeTest.java
public class ShapeTest {
public static void main(String[] args) {
Circle circle = new Circle("red", 5);
Triangle triangle = new Triangle("green", 3, 4, 5);
Rectangle rectangle = new Rectangle("blue", 4, 5);
addOne(circle);
addOne(triangle);
addOne(rectangle);
System.out.println(circle);
System.out.println(triangle);
System.out.println(rectangle);
}
public static void addOne(Shape shape) {
if (shape instanceof Circle) {
Circle circle = (Circle) shape;
circle = new Circle(circle.getColor(), circle.getRadius() + 1);
shape = circle;
} else if (shape instanceof Rectangle) {
Rectangle rectangle = (Rectangle) shape;
rectangle = new Rectangle(rectangle.getColor(), rectangle.getLength() + 1, rectangle.getWidth() + 1);
shape = rectangle;
} else if (shape instanceof Triangle) {
Triangle triangle = (Triangle) shape;
triangle = new Triangle(triangle.getColor(), triangle.getA() + 1, triangle.getB() + 1, triangle.getC() + 1);
shape = triangle;
}
}
}
```
运行结果:
```
Circle [6.0]
Triangle [4.0,5.0,6.0]
Rectangle [5.0,6.0]
```
阅读全文