从具有具体子类的抽象类实现 Comparable 接口
package geometricobject;
public abstract class GeometricObject implements Comparable {
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 static void main(String[] args) {
GeometricObject circle1 = new Circle(1, "Red", true);
GeometricObject circle2 = new Circle(2, "Blue", false);
GeometricObject maxCircle = new Circle();
GeometricObject rect1 = new Rectangle(1, 1, "Red", true);
GeometricObject rect2 = new Rectangle(2, 2, "Blue", false);
GeometricObject maxRect = new Rectangle();
maxCircle = GeometricObject.max(circle1, circle2);
maxRect = GeometricObject.max(rect1, rect2);
System.out.println(maxCircle.toString());
System.out.println(maxRect.toString());
}
public static GeometricObject max (GeometricObject o1, GeometricObject o2){
if (((Comparable)o1).compareTo(o2) > 0 )
return o1;
else
return o2;
}
public interface Comparable {
public int compareTo(GeometricObject o);
}
public int compareTo(GeometricObject o) {
if (this.getArea() > o.getArea())
return 1;
else if (this.getArea() < o.getArea())
return -1;
else
return 0;
}
public abstract double getArea();
}
CIRCLE 类
class Circle extends GeometricObject {
private double radius;
public Circle() {
}
public Circle(double radius){
this.radius = radius;
}
public Circle(double radius, String color, boolean filled){
this.radius = radius;
setColor(color);
setFilled(filled);
}
public double getRadius(){
return radius;
}
public void setRadius(double radius){
this.radius = radius;
}
public double getArea(){
return radius * radius * Math.PI;
}
public double getDiameter(){
return 2 * radius;
}
public double getPerimeter(){
return 2 * radius * Math.PI;
}
public void printCircle() {
System.out.println("The circle is created " + getDateCreated() +
" and the radius is " + radius);
}
}
矩形类
class Rectangle extends GeometricObject {
private double width;
private double height;
public Rectangle() {
}
public Rectangle(double Width, double Height){
this.width = Width;
this.height = Height;
}
public Rectangle(double Width, double Height, String Color, boolean Filled){
this.width = Width;
this.height = Height;
setColor(Color);
setFilled(Filled);
}
public double getWidth(){
return width;
}
public double getHeight(){
return height;
}
public void setHeight(double Height){
this.height = Height;
}
public double getArea(){
return width * height;
}
public double getPerimeter(){
return 2 * (width + height);
}
}
因此,我尝试执行此单独的类,但不断出现此错误。
Exception in thread "main" java.lang.ExceptionInInitializerError
at geometricobject.GeometricObject.main(GeometricObject.java:44)
Caused by: java.lang.RuntimeException: Uncompilable source code - geometricobject.Circle is not abstract and does not override abstract method compareTo(java.lang.Object) in java.lang.Comparable
at geometricobject.Circle.<clinit>(Circle.java:12)
... 1 more
Java Result: 1
package geometricobject;
public abstract class GeometricObject implements Comparable {
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 static void main(String[] args) {
GeometricObject circle1 = new Circle(1, "Red", true);
GeometricObject circle2 = new Circle(2, "Blue", false);
GeometricObject maxCircle = new Circle();
GeometricObject rect1 = new Rectangle(1, 1, "Red", true);
GeometricObject rect2 = new Rectangle(2, 2, "Blue", false);
GeometricObject maxRect = new Rectangle();
maxCircle = GeometricObject.max(circle1, circle2);
maxRect = GeometricObject.max(rect1, rect2);
System.out.println(maxCircle.toString());
System.out.println(maxRect.toString());
}
public static GeometricObject max (GeometricObject o1, GeometricObject o2){
if (((Comparable)o1).compareTo(o2) > 0 )
return o1;
else
return o2;
}
public interface Comparable {
public int compareTo(GeometricObject o);
}
public int compareTo(GeometricObject o) {
if (this.getArea() > o.getArea())
return 1;
else if (this.getArea() < o.getArea())
return -1;
else
return 0;
}
public abstract double getArea();
}
The CIRCLE class
class Circle extends GeometricObject {
private double radius;
public Circle() {
}
public Circle(double radius){
this.radius = radius;
}
public Circle(double radius, String color, boolean filled){
this.radius = radius;
setColor(color);
setFilled(filled);
}
public double getRadius(){
return radius;
}
public void setRadius(double radius){
this.radius = radius;
}
public double getArea(){
return radius * radius * Math.PI;
}
public double getDiameter(){
return 2 * radius;
}
public double getPerimeter(){
return 2 * radius * Math.PI;
}
public void printCircle() {
System.out.println("The circle is created " + getDateCreated() +
" and the radius is " + radius);
}
}
The Rectangle Class
class Rectangle extends GeometricObject {
private double width;
private double height;
public Rectangle() {
}
public Rectangle(double Width, double Height){
this.width = Width;
this.height = Height;
}
public Rectangle(double Width, double Height, String Color, boolean Filled){
this.width = Width;
this.height = Height;
setColor(Color);
setFilled(Filled);
}
public double getWidth(){
return width;
}
public double getHeight(){
return height;
}
public void setHeight(double Height){
this.height = Height;
}
public double getArea(){
return width * height;
}
public double getPerimeter(){
return 2 * (width + height);
}
}
So I have tried to do this separate classes and continually get this error.
Exception in thread "main" java.lang.ExceptionInInitializerError
at geometricobject.GeometricObject.main(GeometricObject.java:44)
Caused by: java.lang.RuntimeException: Uncompilable source code - geometricobject.Circle is not abstract and does not override abstract method compareTo(java.lang.Object) in java.lang.Comparable
at geometricobject.Circle.<clinit>(Circle.java:12)
... 1 more
Java Result: 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将行更改
为
没有泛型,您需要实现的方法是
compareTo(Object)
,而不是compareTo(GeometricObject)
Change the line
to
Without generics, the method you need to implement is
compareTo(Object)
, notcompareTo(GeometricObject)
每当您实现或重写某些方法时,最好使用 @Override 注释。
It's always good to use the
@Override
annotation whenever you are implementing or overriding some method.