gpt4 book ai didi

java - 从抽象类扩展并实现 Comparable 接口(interface)时出错

转载 作者:行者123 更新时间:2023-12-01 18:23:09 25 4
gpt4 key购买 nike

虽然 GeometricObject 没有错误,但 GeoCircle 显示错误,指出 GeoCircle 不是抽象的,并且不会重写抽象方法compareTo(GeometricObject),尽管compareTo 方法没有编写为抽象类//实现类似接口(interface)的抽象类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;
}

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

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

作为引用,添加 @Override 注释可验证方法是否正确重写了父类(super class)方法,该方法可能已被捕获。

关于java - 从抽象类扩展并实现 Comparable 接口(interface)时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27080045/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com