用Python实现工厂模式

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')

猜你喜欢

转载自www.cnblogs.com/python99/p/12311882.html