[英]method does not override or implement a method from a supertype @override compile error
I'm getting an error method does not override or implement a method from a supertype @Override
. 我收到一个错误
method does not override or implement a method from a supertype @Override
该method does not override or implement a method from a supertype @Override
。 I want to print "cannot change capacity of a car" after it prints one car. 我要在打印一辆汽车后打印“无法更改汽车的容量”。 I need to override the
setCapacity
to print this other part. 我需要重写
setCapacity
来打印其他部分。 I believe the code is mostly correct, just not sure why it's not correctly override the setCapacity
method. 我相信代码大部分是正确的,只是不确定为什么它不能正确覆盖
setCapacity
方法。 The final output is: 最终输出为:
New capacity = 1600
Vehicle Info:
capacity = 1600cc
make = Mazda
Cannot change capacity of a car
Vehicle Info:
capacity = 1200cc
make = Holden
type = sedan
model = Barina
My code is: 我的代码是:
class Vehicle { // base class
public void setCapacity(int setCapacity) {
this.capacity = setCapacity;
System.out.println("New Capacity = " + setCapacity);
}
int capacity;
String make;
Vehicle(int theCapacity, String theMake) {
capacity = theCapacity;
make = theMake;
}
void print() {
System.out.println("Vehicle Info:");
System.out.println(" capacity = " + capacity + "cc" );
System.out.println(" make = " + make );
}
}
class Car extends Vehicle {
public String type;
public String model;
public Car(int theCapacity, String theMake, String theType, String theModel) {
super(theCapacity, theMake);
type = theType;
model = theModel;
}
@Override
public void print() {
super.print();
System.out.println(" type = " + type);
System.out.println(" model = " + model);
}
@Override
public void setCapacity() {
super.print();
System.out.println("Cannot change capacity of a car");
}
}
class Task3 {
public static void main (String[]args){
Car car1 = new Car (1200,"Holden","sedan","Barina" );
Vehicle v1 = new Vehicle (1500,"Mazda");
v1.setCapacity(1600);
v1.print();
car1.setCapacity(1600);
car1.print();
}
}
There is a mismatch in the child and parents method signature of setCapacity()
. setCapacity()
的setCapacity()
方法和父代方法签名不匹配。 If you want to override a method from the parent class in the child class then it must have the same signature. 如果要覆盖子类中父类的方法,则它必须具有相同的签名。
Change 更改
public void setCapacity() { //... }
to 至
public void setCapacity(int setCapacity) { // ... }
in the Car
class. 在
Car
课上。
In your code you have missed the parameter setCapacity
and thus the compiler complains. 在您的代码中,您错过了参数
setCapacity
,因此编译器抱怨。
The void setCapacity(int setCapacity)
is not overriden. void setCapacity(int setCapacity)
不被覆盖。 void setCapacity()
and void setCapacity(int setCapacity)
are two different methods. void setCapacity()
和void setCapacity(int setCapacity)
是两种不同的方法。 Therefore generates the @Override
annotation the compile error. 因此,生成
@Override
批注的编译错误。
Regarding the terminology, the setCapacity
is said to be overloaded in this scenario. 关于术语,在这种情况下,
setCapacity
被认为是过载的。
The signature of createCapacity is different in Vehicle and Car classes. 在车辆类和汽车类中,createCapacity的签名不同。 So, there is a compilation error.
因此,存在编译错误。 In Vehicle class you have an argument setCapacity but in the Car class the argument list to the method is empty.
在Vehicle类中,您有一个参数setCapacity,但在Car类中,该方法的参数列表为空。 So, overriding is not possible.
因此,无法覆盖。
@Override
public void setCapacity( int capacity ) { --> **adding this argument here will fix the issue.**
super.print();
System.out.println("Cannot change capacity of a car");
}
public void setCapacity(int setCapacity) {
this.capacity = setCapacity;
System.out.println("New Capacity = " + setCapacity);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.