使geometricobject类可比较
时间: 2023-04-27 11:03:22 浏览: 56
要使GeometricObject类可比较,可以实现Comparable接口并重写compareTo方法。在compareTo方法中,可以根据需要比较GeometricObject对象的某些属性,例如面积、周长等。这样就可以使用Java中的排序算法对GeometricObject对象进行排序了。
相关问题
修改GeometricObject类以实现GeometricObject接口
好的,以下是修改后的GeometricObject类实现GeometricObject接口的代码:
```
public abstract class GeometricObject implements GeometricObjectInterface {
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
protected GeometricObject() {
dateCreated = new java.util.Date();
}
protected GeometricObject(String color, boolean filled) {
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public boolean isFilled() {
return filled;
}
public void setFilled(boolean filled) {
this.filled = filled;
}
public java.util.Date getDateCreated() {
return dateCreated;
}
public String toString() {
return "created on " + dateCreated + "\ncolor: " + color +
" and filled: " + filled;
}
public abstract double getArea();
public abstract double getPerimeter();
}
```
修改后,GeometricObject类实现了GeometricObjectInterface接口,并且实现了接口中的两个抽象方法getArea()和getPerimeter()。
java 设计一个geometricobject类,geometricobject类
可以作为所有几何对象的父类,包含一些基本属性和方法。这个类可以包含以下属性和方法:
属性:
- color:表示几何对象的颜色
- filled:表示几何对象是否被填充
- dateCreated:表示对象创建的日期
方法:
- getColor():获取几何对象的颜色
- setColor():设置几何对象的颜色
- isFilled():检查几何对象是否被填充
- setFilled():设置几何对象是否被填充
- getDateCreated():获取几何对象的创建日期
- toString():返回几何对象的字符串表示
代码实现如下:
```java
public class GeometricObject {
private String color;
private boolean filled;
private Date dateCreated;
public GeometricObject() {
dateCreated = new Date();
}
public GeometricObject(String color, boolean filled) {
dateCreated = new Date();
this.color = color;
this.filled = filled;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public boolean isFilled() {
return filled;
}
public void setFilled(boolean filled) {
this.filled = filled;
}
public Date getDateCreated() {
return dateCreated;
}
public String toString() {
return "created on " + dateCreated + "\ncolor: " + color + " and filled: " + filled;
}
}
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://img-home.csdnimg.cn/images/20210720083646.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)