Java “必须实现继承的抽象方法”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9781564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:36:20  来源:igfitidea点击:

"must implement inherited abstract method"

java

提问by teamaster

I had this original parent abstract class Geometric and i was extending it in Octagon, and also implementing Comparable and Cloneable. IDK why i keep getting the above error.Help with be appreciated.

我有这个原始的父抽象类 Geometric,我在 Octagon 中扩展了它,并且还实现了 Comparable 和 Cloneable。IDK 为什么我不断收到上述错误。不胜感激。

class Octagon extends GeometricObject implements Cloneable, Comparable{
  private double side;
  public class Octagon(){
  }
  public class Octagon(double s){
    side=s;
  }
  public double getArea(){
    return (2+4/Math.sqrt(2))*side*side;
  }
  public double getPerimeter(){
    return 8*side;
  }
  public int compareTo(Object o){
    if (getArea()>((Octagon)o).getArea()){
      return 1;
    }
    else if (getArea()<((Octagon)o).getArea()){
      return -1;
    }
    else 
      return 0;
  }
  public Object clone() throws CloneNotSupportedException{
    super.clone();
  }
}

And this is my Geometric Class

这是我的几何课

public abstract class GeometricObject{
  private String color="white";
  private boolean filled;
  private java.util.Date dateCreated;

  protected GeometricObject(){
    dateCreated=new java.util.Date();
  }

  protected GeometricObject(String color, boolean filled){
    dateCreated=new java.util.Date();
    this.color=color;
    this.filled=filled;
  }

  public String getColor(){
    return color;
  }
  public void setColor(String color){
    this.color=color;
  }
  public boolean isFilled(){
    return filled;
  }
  public void setFilled(boolean filled){
    this.filled=filled;
  }
  public java.util.Date getDateCreated(){
    return dateCreated;
  }
  public String toString() {
    return "created on "+dateCreated+"\ncolor: "+color;
  }
  public abstract double getArea();
  public abstract double getPerimeter();
}

采纳答案by Andreas Dolk

Got it - the error is within you "constructors":

明白了 - 错误在你的“构造函数”中:

public class Octagon(){
//     ^^^^^ <- remove
}

public class Octagon(double s){
//     ^^^^^ <- remove
  side=s;
}

The class"modifiers" are illegal. Simply remove them.

class“调节剂”是非法的。只需删除它们。



Bonus advice - consider changing the implementation of compareTo. You're looking at the octagons area, which is perfectly OK for comparision. But it requires calculating the area each and every time when two octagons need to be compared.

额外建议 - 考虑更改compareTo. 您正在查看八边形区域,这非常适合进行比较。但是当需要比较两个八边形时,它需要每次计算面积。

As the area only depends on the sidevalue, it's sufficient and much more efficient to compare octagons by their sidelength:

由于面积仅取决于side值,因此按side长度比较八边形就足够了且效率更高:

public int compareTo(Object other) {

 if (!(other instanceof Octagon)) {
   throw new IllegalArgumentException("Comparision with other types is not supported");
 }

 Integer thisSide = side;    // autoboxing, legal conversion since Java 1.5
 Integer otherSide = ((Octagon) other).side;     

 // Easy trick: Integer is already comparable ;)
 return thisSide.compareTo(otherSide);
}

回答by mfrankli

When you write "Octagon extends GeometricObject", you are essentially promising that your class can do everything that a GeometricObject can do, and maybe more. So if you're not implementing a method that is part of a GeometricObject, then you're breaking this promise, and the Java compiler objects to this.

当您编写“Octagon extends GeometricObject”时,您基本上是在保证您的类可以完成 GeometricObject 可以完成的所有事情,甚至更多。因此,如果您没有实现属于 GeometricObject 一部分的方法,那么您就违反了这一承诺,并且 Java 编译器对此表示反对。

回答by Marvo

My guess is that GeometricObject is an abstract class. It probably has some abstract methods that need to be implemented in Octogon.

我的猜测是 GeometricObject 是一个抽象类。它可能有一些需要在 Octogon 中实现的抽象方法。

UPDATE:

更新

Found it. I pasted these two classes into Eclipse, and boom:

找到了。我将这两个类粘贴到 Eclipse 中,然后繁荣:

public class Octagon(){
}
public class Octagon(double s){
    side=s;
}

Subtle and tricky typo. Remove the word "class" from those two constructor declarations, and you'll still have an error or two, but they should be obvious.

微妙而棘手的错别字。从这两个构造函数声明中删除“类”一词,您仍然会遇到一两个错误,但它们应该很明显。

回答by Edwin Buck

GeometricObjectis an abstract class, and there is an abstractmethod in the cass GeometricObject(or one of it's super-classes) that you have not provided an implementation.

GeometricObject是一个抽象类,并且abstract在 cass GeometricObject(或它的超类之一)中有一个您没有提供实现的方法。

As a concrete Class cannot be constructed if one of it's abstract methods lacks an implementation, you get the compilation error.

由于如果其中一个抽象方法缺少实现,则无法构造具体类,因此会出现编译错误。

回答by emory

As others have said, you should post the contents of GeometricObject.javafor a definitive answer.

正如其他人所说,您应该发布内容GeometricObject.java以获得明确的答案。

Nevertheless, I have a pretty good idea that it is Comparable. It is properly Comparable<T>but you have Comparablein your code. Try changing your code to class Octagon extends GeometricObject implements Cloneable, Comparable<Object>

尽管如此,我有一个很好的想法,那就是Comparable. 这是正确的,Comparable<T>但您Comparable的代码中有。尝试将您的代码更改为class Octagon extends GeometricObject implements Cloneable, Comparable<Object>