1 class LeiFeng(object): 2 3 def Sweep(self): 4 print("扫地") 5 6 def Wash(self): 7 print("洗衣") 8 9 def BuyRice(self): 10 print("买米") 11 12 13 class IFactory(LeiFeng): 14 15 def CreateLeiFeng(self): 16 pass 17 18 19 # 大学生 20 class Undergraduate(LeiFeng): 21 pass 22 23 24 # 新增社区服务者 25 class Volunteer(LeiFeng): 26 pass 27 28 29 # 学习雷锋的大学生工厂 30 class UndergraduateFactory(IFactory): 31 32 def CreateLeiFeng(self): 33 return Undergraduate() 34 35 36 # 新增一个社区服务者的工厂 37 class VolunteerFactory(IFactory): 38 39 def CreateLeiFeng(self): 40 return Volunteer() 41 42 43 if __name__ == "__main__": 44 student = UndergraduateFactory() 45 volunteer = VolunteerFactory() 46 student.BuyRice() 47 student.Sweep() 48 volunteer.Wash()