定义一个创造汽车的函数来实现分类

# 定义奔驰车类
class BenchiCar(object):
    # 定义车的方法
    def move(self):
        print('---奔驰车在移动---')
    def stop(self):
        print('---奔驰车停车了---')

# 定义宝马车类
class BMWCar(object):
    # 定义车的方法
    def move(self):
        print('---宝马车在移动---')

    def stop(self):
        print('---宝马车停车了---')

# 定义一个函数,来模拟一个汽车厂,目的是创建出具体的汽车对象
def creatCar(typeName):
    if typeName == '奔驰':
        car = BenchiCar()  # 找一辆车
    elif typeName == '宝马':
        car = BMWCar()  # 找一辆车
    return car


# 定义一个销售北京现代车的店类
class CarStore(object):
    def order(self, typeName):
        # 让工厂根据类型,生产一辆汽车
        car = creatCar(typeName)
        return car

pinpai_store = CarStore()
my_car = pinpai_store.order('宝马')
my_car.move()
my_car.stop()

  

猜你喜欢

转载自www.cnblogs.com/wf-skylark/p/9009404.html