在导出类中覆盖了在抽象基类的抽象方法,并在抽象方法中访问在本类定义的属性。
在抽象基类构造函数调用该抽象方法。
abstract class Base {
public Base() {
print();
}
public abstract void print();
}
class Derived extends Base {
private int p = 5;
public void print() {
System.out.println(this + ".print() " + p);
}
@Override
public String toString() {
return "Derived";
}
}
public class ExerciseThird {
public static void main(String[] args) {
new Derived();
}
}
/*Output:
Derived.print() 0
*///:~