class Base {
int value = 0;
Base() {
add();
}
void add() {
value += 10;
}
int get() {
return value;
}
}
class Derived extends Base {
Derived() {
add();
}
void add() {
value += 20;
}
}
当调用派生类构造函数然后add()
时,该值变为40.为什么?Java-超载和重载概念
方法从派生类被调用,为什么不基类方法... – Yogendra123
@ Yogendra123 - 因为这是如何压倒一切的作品!所有对类中的方法和其超类中的方法的调用都成为对override方法的调用。唯一的例外是调用super。'(例如'super.add')或具有相同签名且永不被覆盖的私有方法。 –