java抽象继承-模板方法

//模板方法:步骤提前设计好,用的时候只需要改步骤内容
public class TemplateDemo { public static void main(String[] args) {
// TODO Auto-generated method stub
teacher DBtea = new DBTeacher();
teacher Ctea = new CTeacher();
DBtea.work();
Ctea.work();
}
} abstract class teacher{
public void prepared() {
System.out.println("整理资料");
System.out.println("打开投影仪");
} public abstract void teaching();//抽象方法,子类需重写 public void end() {
System.out.println("关闭投影仪");
System.out.println("带走资料");
}
//老师的工作流程/模板方法
public void work() {
prepared();//课前准备,每个老师都一样
teaching();//讲课
end();//结束,每个老师都一样
}
} class DBTeacher extends teacher{
public void teaching() {
System.out.println("打开oracle");
System.out.println("写pl-sql");
}
} class CTeacher extends teacher{
public void teaching() {
System.out.println("打开VM");
System.out.println("写C指令");
}
}
上一篇:oop面向对象【类与对象、封装、构造方法】


下一篇:Javascript创建类和对象