Class 不是抽象的并且不会覆盖抽象方法错误...虽然它实现了方法
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(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;
}
}
我不知道为什么...我看到这个问题被问了几次,但在大多数情况下,这是由于没有从接口实现某些方法(或者输入错误的方法名称,这不是我的方法名称)将接口方法复制到所有 3 类...
的情况
您有:
interface Shape {
String getShapeName();
double getField();
}
但是,您在 sub-classes 中 明确声明 您覆盖了:
@Override
public double getField(double r) {...}
double getField()
和double getField(double r)
不一样。
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
(即 @Retention(SOURCE)
)注释在你的方法之上,强制你覆盖对应的方法,否则将无法编译你的文件。
我收到了不应显示的错误消息。 首先,在创建接口后,我将其方法复制到所有实现此接口的 类 - 但错误仍然存在。
目前看起来像这样:
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;
}
}
我不知道为什么...我看到这个问题被问了几次,但在大多数情况下,这是由于没有从接口实现某些方法(或者输入错误的方法名称,这不是我的方法名称)将接口方法复制到所有 3 类...
的情况您有:
interface Shape {
String getShapeName();
double getField();
}
但是,您在 sub-classes 中 明确声明 您覆盖了:
@Override
public double getField(double r) {...}
double getField()
和double getField(double r)
不一样。
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
(即 @Retention(SOURCE)
)注释在你的方法之上,强制你覆盖对应的方法,否则将无法编译你的文件。