我是否在代码中正确覆盖了compareTo和toString方法?

时间:2019-04-08 01:25:16

标签: java override comparable implements

我的分配(总而言之)是在我的代码中重写以下方法:compareTo(接口Comparable),toString(GeometricObject类)和equals(Object类)。

我是Java新手,不确定我是否正确使用@override方法。如果您能为我指出正确的方向,请提供反馈。

我用于覆盖所述方法的部分代码:

Circle.java部分代码

//Circle.java
@Override /**Override equals(class Object**/
public boolean equals(Object o) {
    return this.compareTo((Circle)o) == 0;
}

@Override /**Override compareTo(interface Comparable)**/
public int compareTo(Circle o) {
    if (this.radius > o.radius)
        return 1;
    else if (this.radius < o.radius)
        return -1;
    else
        return 0;
}

@Override /**Override toString(class GeometricObject**/
public String toString(GeometricObject o) {
    return super.toString()+"\nDate created:"+getDateCreated()+"\nRadius:"+radius; }

GeometricObject.java部分代码

//GeometricObject.java
/** Return color */
public String getColor() {
    return color;
}

/** Set a new color */
public void setColor(String color) {
    this.color = color;
}

/** Return filled. Since filled is boolean,
 *  the get method is named isFilled */
public boolean isFilled() {
    return filled;
}

/** Set a new filled */
public void setFilled(boolean filled) {
    this.filled = filled;
}

/** Get dateCreated */
public java.util.Date getDateCreated() {
    return dateCreated;
}

@Override
public String toString() {
    return "created on"+dateCreated+"\ncolor:"+color+" and filled:" + filled;
}

/** Abstract method getArea */
public abstract double getArea();

/** Abstract method getPerimeter */
public abstract double getPerimeter();

}

0 个答案:

没有答案