定义抽象draw功能,并为具体图形例如正方形,三角形定义具体绘画具体功能JAVA代码
时间: 2024-06-12 17:05:33 浏览: 11
抽象的draw功能可以定义为:
```
public abstract void draw();
```
对于具体的图形,例如正方形和三角形,可以定义具体的绘画功能。例如:
正方形:
```
public class Square {
private int x;
private int y;
private int size;
public Square(int x, int y, int size) {
this.x = x;
this.y = y;
this.size = size;
}
public void draw() {
// 具体绘画功能,例如使用Graphics绘制
Graphics g = ...;
g.drawRect(x, y, size, size);
}
}
```
三角形:
```
public class Triangle {
private int x1;
private int y1;
private int x2;
private int y2;
private int x3;
private int y3;
public Triangle(int x1, int y1, int x2, int y2, int x3, int y3) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.x3 = x3;
this.y3 = y3;
}
public void draw() {
// 具体绘画功能,例如使用Graphics绘制
Graphics g = ...;
int[] xPoints = {x1, x2, x3};
int[] yPoints = {y1, y2, y3};
g.drawPolygon(xPoints, yPoints, 3);
}
}
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)