父类中可继承方法在处理private的一个demo

时间:2024-01-19 12:35:02
public abstract class AbstractParent {
public AbstractParent() {
System.out.println("Hello,parent");
} protected void earnMoney() {
prepare();
doJob();
} protected abstract void doJob(); private void prepare() {
System.out.println("parent prepare");
}
} class Child extends AbstractParent {
public Child() {
System.out.println("Child init");
} @Override
protected void doJob() {
System.out.println("Child do job");
} public static void main(String[] args) {
Child child = new Child();
child.earnMoney();
}
}

输出:

Hello,parent
Child init
parent prepare
Child do job