Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从抽象类扩展并实现可比较接口时出错_Java - Fatal编程技术网

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

Java 从抽象类扩展并实现可比较接口时出错,java,Java,尽管GeometricObject没有错误,但GeoCircle显示一个错误,表示GeoCircle不是抽象的,并且不会覆盖抽象方法compareTo(GeometricObject),尽管compareTo方法不是作为抽象类编写的 //实现可比较接口的抽象类GeometricObject public abstract class GeometricObject implements Comparable<GeometricObject> { public String

尽管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;
  }
}
公共抽象类GeometricObject实现了
{
公共字符串名称;
//获取各种形状区域的示例抽象类
公共抽象双getArea();
//用于获取各种形状的周长/周长的示例抽象类
公共抽象双GetPermiture();
//传入并返回在系统输出行中选择的对象的名称
公共无效名称(字符串n)
{
System.out.println(“这是一个”+n);
}
/**一种比较两个几何对象面积并为其相等值返回布尔值的方法*/
公共静态布尔equalArea(GeometricObject对象1、GeometricObject对象2)
{
//把双倍与另一个双倍进行比较
返回object1.getArea()==object2.getArea();
}
//查找两个GeometricObject之间的较大值并返回String语句的方法
公共静态最大无效空间(几何对象g1、几何对象g2)
{
如果(g1.与(g2)>0相比)
System.out.println(“对象1较大”);
else if(g1.compareTo(g2)g2.getArea())
返回1;
else if(g1.getArea()obj.getArea())
返回1;
else if(this.getArea()
作为参考,添加@Override注释可验证一个方法是否正确重写了一个超类方法,该超类方法可能已被捕获


作为参考,添加@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) {
        ...
    }
}
class GeometricObjectAreaComparator implements Comparator<GeometricObject> {
    @Override
    public int compare(GeometricObject o1, GeometricObject o2) {
        ...
    }
}
公共抽象类GeometricObject实现了{
...
//从实现的可比较接口重写compareTo方法
公共整数比较(T){
如果(this.getArea()>that.getArea())
返回1;

else如果(this.getArea()您应该在基类中使用泛型:

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) {
        ...
    }
}
class GeometricObjectAreaComparator implements Comparator<GeometricObject> {
    @Override
    public int compare(GeometricObject o1, GeometricObject o2) {
        ...
    }
}
公共抽象类GeometricObject实现了{
...
//从实现的可比较接口重写compareTo方法
公共整数比较(T){
如果(this.getArea()>that.getArea())
返回1;

else if(this.getArea())它看起来像任何两个
GeometricObjects
都可以通过面积进行比较,而不仅仅是圆对圆。它看起来像任何两个
GeometricObjects
都可以通过面积进行比较,而不仅仅是圆对圆。这是一个非常糟糕的主意-可比较的接口需要'e1.compareTo(e2)'==0 e1。等于(e2)“。你的解决方案打破了这一合同-圆圈和正方形可以有相等的面积,但它们不相等。我同意@ursa在这一点上的观点。最好有一个单独的
比较器来进行面积比较。另一方面,如果这是一个课堂作业,并且讲师告诉他们这样做,你就不会有太多问题了。”可以。除了可能重写
equals
作为区域比较。这是一个非常糟糕的主意-可比较接口需要'e1.compareTo(e2)==0 e1.equals(e2)“。你的解决方案打破了这一合同-圆圈和正方形可以有相等的面积,但它们不相等。我同意@ursa在这一点上的观点。最好有一个单独的
比较器来进行面积比较。另一方面,如果这是一个课堂作业,并且讲师告诉他们这样做,你就不会有太多问题了。”可以。除了可能覆盖
equals
作为区域比较。