Java无法重写在构造函数中使用的方法:类型Slime的setupAnimationFrames()方法必须覆盖或实现超类型方法
问题描述:
我在覆盖从Java中的父类继承的方法时遇到问题。从我的父类的摘录:Java无法重写在构造函数中使用的方法:类型Slime的setupAnimationFrames()方法必须覆盖或实现超类型方法
public Character(String spriteLabel, float frameTime, float speed, float x, float y) {
// Setup font and nameplate
this.setupAnimationFrames();
....
this.currentFrame = downFrames[0];
}
private void setupAnimationFrames() {
...
}
在我的孩子上课,我想:
public class Slime extends Character {
@Override
private void setupAnimationFrames() {
...
}
public Slime() {
super("...", 0.15f, 2,727,300);
}
}
然而,Eclipse是告诉我:
The method setupAnimationFrames() of type Slime must override or implement a supertype method
我已经检查并将项目/工作区属性设置为使用Java 1.6,这似乎是此错误的常见原因。
为什么Java不会让我重写该方法?
答
重写是为那些例如方法仅是可见儿童class.Since,超类setupAnimationFrames()
方法是私有的,它不是子类可见,因此压倒一切的不适here.You是在此实现您自己的方法。只需删除@Override
注释以便编译代码。
PS: - 只为了验证目的,你可以实现setupAnimationFrames()
方法与任何返回类型,或者你可以抛出任何checked异常,它将编译(当然有@Override注释)
private String setupAnimationFrames() {
//This compiles in child class,return type is different from parent class
...
}
private List setupAnimationFrames() throws Throwable {
//This compiles in child class ,a checked Throwable has been added to
//method signature
...
}
+0
明白了,谢谢! – BnMcG 2014-10-07 17:05:57
因为它私人,你不能重写一个私人方法。你可以*执行*它,但你不能*覆盖*它。 – 2014-10-07 16:49:01