繁体   English   中英

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

[英]Error extending from an abstract class and implementing the Comparable interface

尽管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;
  }
}
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;
}

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

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

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

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

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) {
        ...
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM