图形均具有绘图方法Draw()和擦除方法Erase(),具体实现
时间: 2024-11-12 15:41:36 浏览: 3
对于具有绘图方法`draw()`和擦除方法`erase()`的具体图形类,例如圆形、矩形和三角形,我们可以分别为每个类实现这两个方法。这里以Java为例给出一个简单的实现:
**Circle 类**
```java
public class Circle implements Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public void draw() {
System.out.println("绘制圆形,半径为:" + radius);
}
@Override
public void erase() {
System.out.println("擦除圆形,半径为:" + radius);
}
}
```
**Rectangle 类**
```java
public class Rectangle implements Shape {
private double width;
private double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
@Override
public void draw() {
System.out.println("绘制矩形,宽为:" + width + ", 高为:" + height);
}
@Override
public void erase() {
System.out.println("擦除矩形,宽为:" + width + ", 高为:" + height);
}
}
```
**Triangle 类**
```java
public class Triangle implements Shape {
private double side1;
private double side2;
private double side3;
public Triangle(double side1, double side2, double side3) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
@Override
public void draw() {
System.out.println("绘制三角形,三边分别为:" + side1 + ", " + side2 + ", " + side3);
}
@Override
public void erase() {
System.out.println("擦除三角形,三边分别为:" + side1 + ", " + side2 + ", " + side3);
}
}
```
以上每个类都有一个构造函数用于初始化形状的属性,然后根据形状特性实现了`draw()`和`erase()`方法。在实际应用中,你可以根据图形的具体需求添加更多的属性和方法。
阅读全文