class CarFactory:
'''用Python实现工厂模式'''
def creatCar(self, car_name):
if car_name == 'Benz':
car = Benz()
elif car_name == 'BMW':
car = BMW()
else:
car = BYD()
return car
class Benz:
def __init__(self):
print('奔驰产生了')
class BMW:
def __init__(self):
print('宝马产生了')
class BYD:
def __init__(self):
print('比亚迪产生了')
cf = CarFactory()
cf.creatCar('123')
cf.creatCar('Benz')
原文地址:https://www.cnblogs.com/python99/p/12311882.html
时间: 2024-11-10 14:16:33