类不是抽象的,也没有覆盖抽象方法的错误… 尽管它实现了该方法

huangapple go评论131阅读模式
英文:

Class is not abstract and does not override abstract method error... though it implements method

问题

我得到了不应该显示的错误消息。
首先,在创建接口之后,我将其方法复制到实现此接口的所有类中,但错误仍然显示。

目前代码如下:

  1. interface Shape {
  2. String getShapeName();
  3. double getField();
  4. }
  5. class Circle implements Shape {
  6. String name;
  7. double r;
  8. public Circle(String name, double r) {
  9. this.name = name;
  10. this.r = r;
  11. }
  12. @Override
  13. public double getField() {
  14. return Math.PI * Math.pow(r, 2);
  15. }
  16. @Override
  17. public String getShapeName() {
  18. return this.name;
  19. }
  20. }
  21. class Square implements Shape {
  22. String name;
  23. double a;
  24. public Square(String name, double a) {
  25. this.name = name;
  26. this.a = a;
  27. }
  28. @Override
  29. public double getField() {
  30. return Math.pow(a, 2);
  31. }
  32. @Override
  33. public String getShapeName() {
  34. return this.name;
  35. }
  36. }
  37. class Triangle implements Shape {
  38. String name;
  39. double a;
  40. double b;
  41. double c;
  42. public Triangle(String name, double a, double b, double c) {
  43. this.name = name;
  44. this.a = a;
  45. this.b = b;
  46. this.c = c;
  47. }
  48. @Override
  49. public double getField() {
  50. double p = (a + b + c) / 2;
  51. return Math.sqrt(p * (p - a) * (p - b) * (p - c));
  52. }
  53. @Override
  54. public String getShapeName() {
  55. return this.name;
  56. }
  57. }

我不知道为什么会出现这种情况... 我看到这个问题已经被问了几次,但在大多数情况下,这是因为没有实现接口中的一些方法(或者方法名称输入错误),但这不适用于我的情况,因为接口方法已复制到所有三个类中...

英文:

I'm getting this error message which should not be showing.
First of all after creating interface I copied its method into all classes that implements this interface - but the error is still showing.

this currently looks like this:

  1. interface Shape {
  2. String getShapeName();
  3. double getField();
  4. }
  5. class Circle implements Shape {
  6. String name;
  7. double r;
  8. public Circle(String name, double r) {
  9. this.name = name;
  10. this.r = r;
  11. }
  12. @Override
  13. public double getField(double r) {
  14. return Math.PI * Math.pow(r, 2);
  15. }
  16. @Override
  17. public String getShapeName() {
  18. return this.name;
  19. }
  20. }
  21. class Square implements Shape {
  22. String name;
  23. double a;
  24. public Square(String name, double a) {
  25. this.name = name;
  26. this.a = a;
  27. }
  28. @Override
  29. public double getField(double a) {
  30. return Math.pow(a, 2);
  31. }
  32. @Override
  33. public String getShapeName() {
  34. return this.name;
  35. }
  36. }
  37. class Triangle implements Shape {
  38. String name;
  39. double a;
  40. double b;
  41. double c;
  42. public Triangle(String name, double a, double b, double c) {
  43. this.name = name;
  44. this.a = a;
  45. this.b = b;
  46. this.c = c;
  47. }
  48. @Override
  49. public double getField(double a, double b, double c) {
  50. double p = (a + b + c) / 2;
  51. return Math.sqrt(p * (p - a) * (p - b) * (p - c));
  52. }
  53. @Override
  54. public String getShapeName() {
  55. return this.name;
  56. }
  57. }

And I have no idea why... I saw that this question was asked a few times but in most cases it was a result of not implementing some methods from interface (or a wrong typing method name which are not my cases as the interface methods were copied to all 3 classes...

答案1

得分: 1

  1. interface Shape {
  2. String getShapeName();
  3. double getField();
  4. }

然而,在你的子类中,你明确地声明你要覆盖:

  1. @Override
  2. public double getField(double r) {...}

double getField()double getField(double r) 不是相同的。

覆盖与隐藏方法:
>子类中具有与超类中实例方法相同的签名(名称,加上参数的数量和类型)和返回类型的实例方法将覆盖超类的方法。

附注:@Override(它是@Retention(SOURCE))注解在你的方法顶部,强制覆盖相应的方法,否则你将无法编译你的文件。

英文:

You have:

  1. interface Shape {
  2. String getShapeName();
  3. double getField();
  4. }

however, you explicitly state in your sub-classes, that you override:

  1. @Override
  2. public double getField(double r) {...}

double getField() and double getField(double r) are not the same.

Overriding and Hiding Methods:
>An instance method in a subclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the superclass overrides the superclass's method.

Side-note: @Override (which is @Retention(SOURCE)) annotation on top of your method, enforces you to override corresponding method, otherwise you will not be able to compile your file.

huangapple
  • 本文由 发表于 2020年10月19日 23:46:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/64430786.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定