打电话给超级
class A {
int foo () { return 2; }
}
class B extends A {
boolean someCondition;
public B(boolean b) { someCondition = b; }
int foo () {
if(someCondition) return super.foo();
return 3;
}
}
Run Code Online (Sandbox Code Playgroud)
这super是为了什么.如果重写方法method,那么您可以像这样实现它:
protected void method() {
if (special_conditions()) {
super.method();
} else {
// do your thing
}
}
Run Code Online (Sandbox Code Playgroud)