package exer1; //工厂方法的设计模式(创建对象的时机) public class TestFactoryMethod { public static void main(String[] args) { IWorkFactory i=new TeacherWorkFactory();//多态性 IWorkFactory i1=new StudentWorkFactory(); i.getWork().doWork(); i1.getWork().doWork(); } } interface IWorkFactory{ Work getWork(); } class TeacherWorkFactory implements IWorkFactory{ public Work getWork(){ return new TeacherWork();//注意返回的new TeacherWork(),下文有过的 } } class StudentWorkFactory implements IWorkFactory{ public Work getWork(){ return new StudentWork(); } } interface Work{ void doWork(); } class StudentWork implements Work{ public void doWork(){ System.out.println("学生写作业"); } } class TeacherWork implements Work{ public void doWork(){ System.out.println("老师批改作业"); } }