- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我收到错误方法没有重写或实现父类(super class)型@Override 中的方法
。我想在打印一辆车后打印“无法更改汽车的容量”。我需要重写 setCapacity 来打印其他部分。我相信代码大部分是正确的,只是不确定为什么它没有正确覆盖 setCapacity
方法。最终输出为:
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
我的代码是:
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();
}
}
最佳答案
setCapacity()
的子方法和父方法签名不匹配。如果您想在子类中重写父类的方法,那么它必须具有相同的签名。
改变
public void setCapacity() { //... }
到
public void setCapacity(int setCapacity) { // ... }
在 Car
类中。
在您的代码中,您错过了参数setCapacity
,因此编译器会提示。
关于java - 方法不重写或实现父类(super class)型中的方法 @override 编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52855550/
有没有在OpenJDK 1.7.0_45中派生类重写基类方法,但没有@Override注解,运行时派生类实例调用基类方法的情况? class Base { public f() { }
我正在尝试在 C++ 中练习 OOP,但我遇到了有关覆盖函数的问题。在我的 Shape2D 和 Shape3D 类中,我有在 Square 和 Sphere 类(分别为 ShowArea() 和 Sh
我想控制值在槽中的保存方式以及读取槽时返回的内容。这是我的类定义: (defclass object () ((name :accessor name-access :initf
我正在尝试在 C++ 中练习 OOP,但我遇到了有关覆盖函数的问题。在我的 Shape2D 和 Shape3D 类中,我有在 Square 和 Sphere 类(分别为 ShowArea() 和 Sh
我读了section在 Scala 编程中,引入了抽象重写,但我仍然对这些修饰符的连接到底意味着什么感到困惑。使用这些修饰符的代码片段粘贴在下面: trait Doubling extends Int
阅读Javadoc对于 @Override 注释,我遇到了以下规则: If a method is annotated with thisannotation type compilers are r
我正在基于 BEP20Token 模板 (https://github.com/binance-chain/bsc-genesis-contract/blob/master/contracts/bep
关于下面提到的 3 份契约(Contract): 1) Whenever hashCode() is invoked on the same object more than once during
在 C# 中,override 默认启用,那么,是否不需要显式在基类中将方法声明为可覆盖?如果是的话 Overridable 仅限于 VB.NET 还是在 C# 中也是必需的? 因此可以覆盖哪些类型的
以下代码在 public void onClick 行生成此错误消息。 Multiple markers at this line - implements android.view.View.OnC
当我在运行 IIS 的服务器 2012R2 上托管它时,我能够使用 Autorest 和我的 api 生成代码 但是,当我尝试使用 localhost url 运行它时,我收到一条无法读取的消息。 我
代码如下。 IDE 的代码没问题,但 gradle 拒绝构建,并表示: TextAdapter is not abstract and does not override abstract metho
这个问题已经有答案了: Best practice for overriding classes / properties in ExtJS? (3 个回答) 已关闭 8 年前。 这两个覆盖有什么区别
我今天将 xcode 更新为 7。更新后,我正在处理的项目出现警告“覆盖成员函数但未标记为‘覆盖’”。由于我们的项目将“踩踏警告为错误”设置为true。我遇到了很多错误。 我仔细检查了“Other L
我试图将 Apple 的 ARKit 示例应用程序集成到我的应用程序中。由于 ARKit 只是一个附加功能,所以我需要支持较低版本的 iOS。我在所有 ARKit 示例应用程序类中添加了 @avail
我覆盖了类的 Equals() 来比较 Guid 类型的 ID 值。 然后 Visual Studio 警告: ... overrides Object.Equals(object o) but do
我正在尝试用 Java 中的 Runnable 对象创建一个基本线程。下面是我的代码: import java.lang.Thread; import java.lang.Runnable; publ
我有一个函数: int function(int a, int b = 1, int c = 2){ return a+b+c; } 我想将“c”变量的值设置为3,但不想设置“b”的值 在像
我正在尝试了解GAS的.code16行为。 在手册中,对于16位部分,对于32位操作数或指令,似乎会为指令编码生成一个66H操作数替代前缀。这是否意味着 .code16 movw %eax, %ebx
我正在尝试创建一个 JFrame,向 JFrame 添加一个 JLabel(image),但这需要我抛出 IOException,这会弄乱我的 main 方法中的 run() 。 谁能告诉我如何抛出异
我是一名优秀的程序员,十分优秀!