从抽象类扩展并实现Comparable接口时出错

时间:2014-11-22 17:01:59

标签: java

虽然GeometricObject没有错误,但是GeoCircle显示错误,说GeoCircle不是抽象的,并且不会覆盖抽象方法compareTo(GeometricObject),尽管compareTo方法不是作为抽象类编写的 //抽象类GeometricObject,它实现了可比较的接口

public abstract class GeometricObject implements Comparable<GeometricObject>
{

    public String name;
    //sample abstract class of getting area of various shapes

    public abstract double getArea();
    //sample abstract class for getting perimeter/circumference of various shapes
    public abstract double getPerimeter();
    //pass in and return name of the object selected in a system out line
    public void name(String n)
    {
        System.out.println("This is a " + n);
    }


/** A method for comparing the areas of two geometric objects and returning a boolean for their equals */
    public static boolean equalArea(GeometricObject object1,GeometricObject object2)
    {
        //comparing double to another double
        return object1.getArea()==object2.getArea();
    }

    // a method to find the bigger between two GeometricObjects and returning a String statement 
    public static void max(GeometricObject g1, GeometricObject g2)
    {
        if(g1.compareTo(g2)>0)
            System.out.println("Object 1 is larger ");
        else if (g1.compareTo(g2)<0)
            System.out.println("Object 2 is larger ");
        else
            System.out.println("Objects are the same ");
    }
    // an override of the compareTo method from the implemented comparable interface
    public int compareTo(GeometricObject g1, GeometricObject g2)
    {
        if(g1.getArea()>g2.getArea())
            return 1;
        else if (g1.getArea()<g2.getArea())
            return -1;
        else
            return 0;
    }
}


//a class for calculating circumference and area of a circle extended from GeometricObject
public class GeoCircle extends GeometricObject implements Comparable<GeoCircle>
{
    public String name;
    public double radius;

    //constructor for only inputting radius of the circle
    public GeoCircle(double r)
    {
        radius = r;
    }
   // 2ndconstructor taking a name for the shape and radius of the circle
    public GeoCircle(String n, double r)
    {
        name = n;
        radius = r;
    }

    //method to get area of the shape with previously passed in radius
    public double getArea()
    {
       return Math.PI*Math.pow(radius,2);
    }

    //method to get circumference of the circle with radius previously given
     public double getPerimeter()
    {
       return 2*Math.PI*radius;
    }

    //a compareTo method

    public int compareTo(GeoCircle obj) 
    {
    if (this.getArea() > obj.getArea())
      return 1;
    else if (this.getArea() < obj.getArea())
      return -1;
    else
      return 0;
  }
}

2 个答案:

答案 0 :(得分:4)

public int compareTo(GeometricObject g1, GeometricObject g2)
{
    if(g1.getArea()>g2.getArea())
        return 1;
    else if (g1.getArea()<g2.getArea())
        return -1;
    else
        return 0;
}

未正确覆盖compareTocompareTo应该采用一个参数并将this与该参数进行比较。这可以实现为

@Override public int compareTo(GeometricObject g) {
  return Double.compare(getArea(), g.getArea());
}

作为参考,添加@Override注释会验证方法是否正确覆盖了可能已被捕获的超类方法。

答案 1 :(得分:0)

您应该在基类中使用泛型:

public abstract class GeometricObject<T extends GeometricObject> implements Comparable<T> {
    ...
    // an override of the compareTo method from the implemented comparable interface
    public int compareTo(T that) {
        if(this.getArea()>that.getArea())
            return 1;
        else if (this.getArea()<that.getArea())
            return -1;
        else
            return 0;
    }
}


//a class for calculating circumference and area of a circle extended from GeometricObject
public class GeoCircle extends GeometricObject<GeoCircle> {
    ...

    @Override // Remove this method if it doesn't differ from parent implementation
    public int compareTo(GeoCircle that) {
        ...
    }
}

可比较的界面非常严格。更好的解决方案是实现单独的比较器并从基类中删除Comparable声明:

class GeometricObjectAreaComparator implements Comparator<GeometricObject> {
    @Override
    public int compare(GeometricObject o1, GeometricObject o2) {
        ...
    }
}