标签:test system 方法 print teacher public rgs void ret
//接口的应用:工厂方法的设计模式
public class TestFactoryMethod {
public static void main(String[] args) {
IWorkFactory i = new StudentWorkFactory();
i.getWork().doWork();
IWorkFactory i1 = new TeacherWorkFactory();
i1.getWork().doWork();
}
}
interface IWorkFactory{
Work getWork();
}
class StudentWorkFactory implements IWorkFactory{
@Override
public Work getWork() {
return new StudentWork();
}
}
class TeacherWorkFactory implements IWorkFactory{
@Override
public Work getWork() {
return new TeacherWork();
}
}
interface Work{
void doWork();
}
class StudentWork implements Work{
@Override
public void doWork() {
System.out.println("学生写作业");
}
}
class TeacherWork implements Work{
@Override
public void doWork() {
System.out.println("老师批改作业");
}
}
标签:test system 方法 print teacher public rgs void ret
原文地址:https://www.cnblogs.com/liyao0312/p/11627457.html