【发布时间】:2011-01-20 20:03:27
【问题描述】:
只是尝试扩展 View 并做一些自定义工作,但是当我尝试覆盖 setFrame 方法时 Eclipse 会抱怨。声称父类中没有要覆盖的方法:
Test 类型的方法 setFrame(int, int, int, int) 必须重写或实现超类型方法
这是来自 android SDK 源的方法的签名。
protected boolean setFrame(int left, int top, int right, int bottom)
如您所见,它不是私有或包级别,甚至指定为最终...只是受保护。这应该意味着我完全能够在子类中覆盖它。对?以下是我在 Eclipse 中尝试做的最低限度的工作。也许这只是一个 Eclipse 错误,但我不太熟悉使用 Ant 来检查它。
编辑:对于那些回答 setFrame 未在 View 类中定义的人,我可以向您保证它是。你认为我怎么得到方法签名?它甚至在 layout() 期间被调用。还是我真的疯了?
git 头:View.java
纸杯蛋糕(1.5r4):View.java
您甚至可以在ImageView 和TextView 类中看到方法被覆盖...这就是为什么我对自己不能直接从View 覆盖它感到非常困惑的原因。 ..
public class Test extends View {
public Test(Context context) {
super(context);
}
public Test(Context context, AttributeSet attrs) {
super(context, attrs);
}
public Test(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected boolean setFrame(int left, int top, int right, int bottom) {
return super.setFrame(left, top, right, bottom);
}
}
【问题讨论】:
标签: java android inheritance extension-methods overriding