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

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

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

# 定义一个销售北京现代车的店类
class CarStore(object):
    def order(self, typeName):
        if typeName == '奔驰':
            car = BenchiCar()   # 找一辆车
        elif typeName == '宝马':
            car = BMWCar()   # 找一辆车
        return car

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

# 这样做,不太好,因为当北京现代又生产一种新类型的车时,那么又得在CarStore类中修改

  

相关文章:

  • 2021-05-26
  • 2021-07-08
  • 2021-04-23
  • 2022-12-23
  • 2021-07-20
  • 2022-12-23
  • 2022-12-23
  • 2021-12-19
猜你喜欢
  • 2022-12-23
  • 2021-07-04
  • 2021-11-02
  • 2022-12-23
  • 2022-12-23
  • 2021-07-14
  • 2021-08-12
相关资源
相似解决方案