aro*_*oth 40
没有办法直接要求这个.但是,你可以做的是:
public class MySuperclass {
public final void myExposedInterface() {
//do the things you always want to have happen here
overridableInterface();
}
protected void overridableInterface() {
//superclass implemention does nothing
}
}
public class MySubclass extends MySuperclass {
@Override
protected void overridableInterface() {
System.out.println("Subclass-specific code goes here");
}
}
Run Code Online (Sandbox Code Playgroud)
这提供了一个内部接口点,子类可以使用它向公共myExposedInterface()
方法添加自定义行为,同时确保始终执行超类行为,无论子类做什么.
wil*_*ill 11
看看findBugs项目......
示例......
import javax.annotation.OverridingMethodsMustInvokeSuper;
:
@OverridingMethodsMustInvokeSuper
protected String getLabel(){
...
}
Run Code Online (Sandbox Code Playgroud)
覆盖:
@Override
protected String getLabel(){
// no call to Super Class!!
// gives a message
...
}
Run Code Online (Sandbox Code Playgroud)
我已经使用它大约3年了.没有小猫受到伤害.
归档时间: |
|
查看次数: |
9904 次 |
最近记录: |