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

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

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

问题

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

目前代码如下:

interface Shape {
    String getShapeName();

    double getField();
}

class Circle implements Shape {
    String name;
    double r;

    public Circle(String name, double r) {
        this.name = name;
        this.r = r;
    }

    @Override
    public double getField() {
        return Math.PI * Math.pow(r, 2);
    }

    @Override
    public String getShapeName() {
        return this.name;
    }
}

class Square implements Shape {
    String name;
    double a;

    public Square(String name, double a) {
        this.name = name;
        this.a = a;
    }

    @Override
    public double getField() {
        return Math.pow(a, 2);
    }

    @Override
    public String getShapeName() {
        return this.name;
    }
}

class Triangle implements Shape {
    String name;
    double a;
    double b;
    double c;

    public Triangle(String name, double a, double b, double c) {
        this.name = name;
        this.a = a;
        this.b = b;
        this.c = c;
    }

    @Override
    public double getField() {
        double p = (a + b + c) / 2;
        return Math.sqrt(p * (p - a) * (p - b) * (p - c));
    }

    @Override
    public String getShapeName() {
        return this.name;
    }
}

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

英文:

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:

interface Shape {
String getShapeName();
double getField();
}
class Circle implements Shape {
String name;
double r;
public Circle(String name, double r) {
this.name = name;
this.r = r;
}
@Override
public double getField(double r) {
return Math.PI * Math.pow(r, 2);
}
@Override
public String getShapeName() {
return this.name;
}
}
class Square implements Shape {
String name;
double a;
public Square(String name, double a) {
this.name = name;
this.a = a;
}
@Override
public double getField(double a) {
return Math.pow(a, 2);
}
@Override
public String getShapeName() {
return this.name;
}
}
class Triangle implements Shape {
String name;
double a;
double b;
double c;
public Triangle(String name, double a, double b, double c) {
this.name = name;
this.a = a;
this.b = b;
this.c = c;
}
@Override
public double getField(double a, double b, double c) {
double p = (a + b + c) / 2;
return Math.sqrt(p * (p - a) * (p - b) * (p - c));
}
@Override
public String getShapeName() {
return this.name;
}
}

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

interface Shape {
    String getShapeName();
    double getField();
}

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

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

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

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

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

英文:

You have:

interface Shape {
    String getShapeName();
    double getField();
}

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

@Override
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:

确定