首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从抽象类扩展并实现可比较接口时出错

从抽象类扩展并实现可比较接口时出错
EN

Stack Overflow用户
提问于 2014-11-23 01:01:59
回答 2查看 835关注 0票数 0

尽管GeometricObject没有错误,但GeoCircle显示了一个错误,指出GeoCircle不是抽象的,并且没有覆盖抽象方法compareTo( GeometricObject ),尽管compareTo方法没有编写为实现可比较接口的抽象类//抽象类GeometricObject

代码语言:javascript
复制
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;
  }
}
EN

回答 2

Stack Overflow用户

发布于 2014-11-23 01:04:05

代码语言:javascript
复制
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与该参数进行比较。这可以实现为

代码语言:javascript
复制
@Override public int compareTo(GeometricObject g) {
  return Double.compare(getArea(), g.getArea());
}

作为参考,添加@Override注解将验证方法是否正确地覆盖了超类方法,而超类方法本应被捕获。

票数 4
EN

Stack Overflow用户

发布于 2014-11-23 01:14:24

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

代码语言:javascript
复制
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) {
        ...
    }
}

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

代码语言:javascript
复制
class GeometricObjectAreaComparator implements Comparator<GeometricObject> {
    @Override
    public int compare(GeometricObject o1, GeometricObject o2) {
        ...
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27080045

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档