编写一个名为TwoDimensionalShape的抽象类,拥有属性area和circumference表示面积和周长,以及抽象方法getArea()和getCircumference(),用于获取面积和周长。 2、 编写Printable接口,包括一个抽象方法printShapeInfo,用于输出图形信息。 3、 分别编写Rectangle、Triangle、Circular三个类,用于描述矩形、三角形和圆形,要求继承于TwoDimensionalShap类,并实现Printable接口,每个图形有各自的构造方法,矩形有length和width字段表示长和宽,三角形有base、hypotenus1和hypotenus2字段表示底和两条斜边,圆形有radius字段表示半径,按几何常识实现父类中所定义的抽象方法,printShapeInfo方法要求能够输出: (1) 图形的类型(使用getClass方法) (2) 图形的基本数据(使用自定义toString方法,输出图形的字段,输出示例:宽3.0, 长4.0) 4、 编写一个名为ShapeDetector的类,拥有三个重载的detectShape方法,分别为detectShape(Rectangle r)、detectShape(Triangle t)、detectShape(Circular c),分别输出所接收到的图形的类型。 5、 编写Test类,创建一个名为Shapes的TwoDimensionalShape型数组,并创建Rectangle、Triangle、Circular的对象存储在该数组中,用foreach循环遍历该数组,使用detectShape方法输出该对象的图形类型,用printShapeInfo方法输出该对象的图形信息,然后分别用getArea和getCircumference方法输出面积及周长。
时间: 2024-03-24 19:41:58 浏览: 98
5.2+纯虚函数与抽象类1
好的,以下是符合你要求的代码实现:
```java
// TwoDimensionalShape.java
public abstract class TwoDimensionalShape {
protected double area;
protected double circumference;
public abstract void getArea();
public abstract void getCircumference();
}
// Printable.java
public interface Printable {
void printShapeInfo();
}
// Rectangle.java
public class Rectangle extends TwoDimensionalShape implements Printable {
private double length;
private double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
getArea();
getCircumference();
}
@Override
public void getArea() {
area = length * width;
}
@Override
public void getCircumference() {
circumference = (length + width) * 2;
}
@Override
public void printShapeInfo() {
System.out.println("Shape Type: " + getClass().getSimpleName());
System.out.println("Shape Data: " + length + "x" + width);
}
}
// Triangle.java
public class Triangle extends TwoDimensionalShape implements Printable {
private double base;
private double hypotenus1;
private double hypotenus2;
public Triangle(double base, double hypotenus1, double hypotenus2) {
this.base = base;
this.hypotenus1 = hypotenus1;
this.hypotenus2 = hypotenus2;
getArea();
getCircumference();
}
@Override
public void getArea() {
double p = (base + hypotenus1 + hypotenus2) / 2;
area = Math.sqrt(p * (p - base) * (p - hypotenus1) * (p - hypotenus2));
}
@Override
public void getCircumference() {
circumference = base + hypotenus1 + hypotenus2;
}
@Override
public void printShapeInfo() {
System.out.println("Shape Type: " + getClass().getSimpleName());
System.out.println("Shape Data: base=" + base + ", hypotenus1=" + hypotenus1 + ", hypotenus2=" + hypotenus2);
}
}
// Circular.java
public class Circular extends TwoDimensionalShape implements Printable {
private double radius;
public Circular(double radius) {
this.radius = radius;
getArea();
getCircumference();
}
@Override
public void getArea() {
area = Math.PI * radius * radius;
}
@Override
public void getCircumference() {
circumference = 2 * Math.PI * radius;
}
@Override
public void printShapeInfo() {
System.out.println("Shape Type: " + getClass().getSimpleName());
System.out.println("Shape Data: radius=" + radius);
}
}
// ShapeDetector.java
public class ShapeDetector {
public void detectShape(Rectangle r) {
System.out.println("Detected Shape Type: " + r.getClass().getSimpleName());
}
public void detectShape(Triangle t) {
System.out.println("Detected Shape Type: " + t.getClass().getSimpleName());
}
public void detectShape(Circular c) {
System.out.println("Detected Shape Type: " + c.getClass().getSimpleName());
}
}
// Test.java
public class Test {
public static void main(String[] args) {
TwoDimensionalShape[] shapes = new TwoDimensionalShape[3];
shapes[0] = new Rectangle(4.0, 3.0);
shapes[1] = new Triangle(3.0, 4.0, 5.0);
shapes[2] = new Circular(2.0);
ShapeDetector detector = new ShapeDetector();
for (TwoDimensionalShape shape : shapes) {
detector.detectShape(shape);
shape.printShapeInfo();
System.out.println("Area: " + shape.area);
System.out.println("Circumference: " + shape.circumference);
}
}
}
```
这份代码实现了上述要求,其中抽象类 `TwoDimensionalShape` 定义了面积和周长属性以及抽象方法,接口 `Printable` 定义了输出图形信息的抽象方法,三个具体的子类 `Rectangle`、`Triangle` 和 `Circular` 继承 `TwoDimensionalShape` 并实现 `Printable` 接口,`ShapeDetector` 类定义了三个重载的方法来识别不同类型的图形,`Test` 类则用于测试代码的运行结果。
阅读全文