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
时间: 2024-10-06 10:30:00