Python学习笔记——面向对象3
2018-03-20 10:02
477 查看
练习:设计类
设计一个卖车的4S店,该怎样做呢?# 定义车类 class Car(object): # 定义车的方法 def move(self): print("---车在移动---") def stop(self): print("---停车---") # 定义一个销售车的店类 class CarStore(object): def order(self): self.car = Car() #找一辆车 self.car.move() self.car.stop()设计一个卖北京现代车的4S店(有很多北京现代品牌的车,比如伊兰特、索纳塔等),该怎样做呢?
# 定义伊兰特车类 class YilanteCar(object): # 定义车的方法 def move(self): print("---车在移动---") def stop(self): print("---停车---") # 定义索纳塔车类 class SuonataCar(object): # 定义车的方法 def move(self): print("---车在移动---") def stop(self): print("---停车---") # 定义一个销售北京现代车的店类 class CarStore(object): def __init__(self, typeName): self.typeName = typeName def order(self): if self.typeName == "伊兰特": self.car = YilanteCar() elif self.typeName == "索纳塔": self.car = SuonataCar() self.car.move() self.car.stop()这样做,不太好,因为当北京现代又生产一种新类型的车时,又得在CarStore类中修改,有没有好的解决办法呢?
工厂模式
在“设计一个类”一节中,最后留下的个问题,该怎样解决呢?
# 定义伊兰特车类 class YilanteCar(object): # 定义车的方法 def move(self): print("---车在移动---") def stop(self): print("---停车---") # 定义索纳塔车类 class SuonataCar(object): # 定义车的方法 def move(self): print("---车在移动---") def stop(self): print("---停车---") # 定义一个生产汽车的工厂,让其根据具体的订单生产车 class CarFactory(object): def createCar(self,typeName): self.typeName = typeName if self.typeName == "伊兰特": self.car = YilanteCar() elif self.typeName == "索纳塔": self.car = SuonataCar() return self.car # 定义一个销售北京现代车的店类 class CarStore(object): def __init__(self): self.carFactory = CarFactory() def order(self, typeName): # 让工厂根据类型,生产一辆汽车 self.car = self.carFactory.createCar(typeName) self.car.move() self.car.stop()咋一看来,好像只是把生产环节重新创建了一个类,这确实比较像是一种编程习惯,但是此种解决方式确实被称作
工厂模式,
简单工厂模式
简单工厂模式案例:蛋糕店
class AppleCake(object): def __init__(self, weidao="苹果味道"): self.taste = weidao class OrangeCake(object): def __init__(self, weidao="橘子味道"): self.taste = weidao class CakeFactory(object): def createCake(self, weidao): if weidao == "橘子": cake = OrangeCake() elif weidao == "苹果": cake = AppleCake() return cake class CakeStore(object): def __init__(self): self.factory = CakeFactory() def taste(self, weidao): cake = self.factory.createCake(weidao) print("------品尝味道:%s----"%cake.taste) a = CakeStore() a.taste("橘子")
多种品牌的汽车4S店
当买车时,有很多种品牌可以选择,比如北京现代、别克、凯迪拉克、特斯拉等,那么此时该怎样进行设计呢?# 定义一个基本的4S店类 class CarStore(object): #仅仅是定义了有这个方法,并没有实现,具体功能,这个需要在子类中实现 def createCar(self, typeName): pass def order(self, typeName): # 让工厂根据类型,生产一辆汽车 self.car = self.createCar(typeName) self.car.move() self.car.stop() # 定义一个北京现代4S店类 class XiandaiCarStore(CarStore): def createCar(self, typeName): self.carFactory = CarFactory() return self.carFactory.createCar(typeName) # 定义伊兰特车类 class YilanteCar(object): # 定义车的方法 def move(self): print("---车在移动---") def stop(self): print("---停车---") # 定义索纳塔车类 class SuonataCar(object): # 定义车的方法 def move(self): print("---车在移动---") def stop(self): print("---停车---") # 定义一个生产汽车的工厂,让其根据具体得订单生产车 class CarFactory(object): def createCar(self,typeName): self.typeName = typeName if self.typeName == "伊兰特": self.car = YilanteCar() elif self.typeName == "索纳塔": self.car = SuonataCar() return self.car suonata = XiandaiCarStore() suonata.order("索纳塔")
最后来看看工厂方法
的定义
定义了一个创建对象的接口(可以理解为函数),但由子类决定要实例化的类是哪一个,
工厂方法让类的实例化推迟到子类,抽象的CarStore提供了一个创建对象的方法createCar,也叫作
工厂方法。子类真正实现这个createCar方法创建出具体产品。 创建者类不需要直到实际创建的产品是哪一个,选择了使用了哪个子类,自然也就决定了实际创建的产品是什么。
__new__
方法
class A(object): def __init__(self): print("这是 init 方法") def __new__(cls): print("这是 new 方法") return object.__new__(cls) A()
总结
__new__至少要有一个参数cls,代表要实例化的类,此参数在实例化时由Python解释器自动提供
__new__必须要有返回值,返回实例化出来的实例,这点在自己实现
__new__时要特别注意,可以return父类
__new__出来的实例,或者直接是object的
__new__出来的实例
__init__有一个参数self,就是这个
__new__返回的实例,
__init__在
__new__的基础上可以完成一些其它初始化的动作,
__init__不需要返回值
我们可以将类比作制造商,
__new__方法就是前期的原材料购买环节,
__init__方法就是在有原材料的基础上,加工,初始化商品环节。
class Test(object): #初始化的功能 #往往是完成对象的属性设置 def __init__(self): self.num = 100 print("-----init----") print(self) #完成创建一个对象 #当a=Test()执行的时候,是先调用__new__方法完成创建对象,然后会紧接着 #调用__init__方法,完成初始化的功能 def __new__(cls): print("----new-----") print(cls) return super().__new__(cls) #def __str__(self): # return "xxxxxx" a = Test() print(a.num) print(a)
单例模式
# 实例化一个单例 class Singleton(object): __instance = None __first_init = False def __new__(cls, age, name): if not cls.__instance: cls.__instance = object.__new__(cls) return cls.__instance def __init__(self, age, name): if not self.__first_init: self.age = age self.name = name Singleton.__first_init = True a = Singleton(18, "dongGe") b = Singleton(8, "dongGe") print(id(a)) print(id(b)) print(a.age) print(b.age) a.age = 19 print(b.age)运行结果:
相关文章推荐
- python学习笔记-Day06-Day07(面向对象)
- Python学习笔记-初级(五):面向对象
- Python学习笔记6:面向对象(一)
- 【python学习笔记】Python面向对象的理解(封装,继承,多态)
- Python面向对象的编程--Python学习笔记五
- Python学习笔记十一:面向对象
- Python学习笔记三:面向对象
- 【python 面向对象】 python对象学习笔记《1》
- Python学习笔记(三)——模块|面向对象
- python学习笔记-Day08--(面向对象)--补充
- Python学习笔记之面向对象
- Dive in Python学习笔记五:对象和面向对象
- 【Python】学习笔记八:面向对象
- python学习笔记(面向对象)
- python学习笔记-(8)初识python面向对象
- python学习笔记之面向对象、基本I/O操作(一)
- python学习笔记(三)面向对象
- Python3学习笔记04-循环、函数、面向对象
- Python学习笔记-面向对象篇
- python学习笔记1:面向对象的高级特性1__slots__