面向对象
1.组合与继承
(1)组合
组合是has-a关系,即一个对象有哪些模块或由哪些模块组成,组成对象的各个模块有各自的功能和作用
举个例子 电脑由CPU、GPU、内存、硬盘、电源、显示器、鼠标、键盘等模块组成,每个模块负责自己的分工
class Computer(object):
def __init__(self):
print("Computer init...")
self.cpu = Cpu()
self.gpu = Gpu()
self.power = Power()
self.memory = Memory()
self.screen = Screen()
self.mouse = Mouse()
self.keyboard = Keyboard()
print("Computer init compulete/finish")
def infor(self):
print("Computer")
class Cpu(object):
def __init__(self):
print("CPU init...")
class Gpu(object):
def __init__(self):
print("GPU init...")
class Power(object):
def __init__(self):
print("Power init...")
class Memory(object):
def __init__(self):
print("Memory init...")
class Screen(object):
def __init__(self):
print("Screen init...")
class Mouse(object):
def __init__(self):
print("Mouse init...")
class Keyboard(object):
def __init__(self):
print("Keyboard init...")
c1 = Computer()
''' 运行结果
Computer init...
CPU init...
GPU init...
Power init...
Memory init...
Screen init...
Mouse init...
Keyboard init...
Computer init compulete/finish
'''
(2)继承 is-a关系
继承是is-a关系,即一个对象是另一个对象,对象都具备相同的基本属性和行为
举个例子 苹果电脑是电脑,惠普电脑也是电脑,华硕电脑也是电脑,他们都具备电脑的基本属性和行为
class Apple(Computer):
def __init__(self,brand):
print("----------APPLE------------")
Computer.__init__(self)
self.brand = brand
print("Welcome use Apple computer")
class HuiPu(Computer):
def __init__(self,brand):
print("----------HUIPU------------")
Computer.__init__(self)
self.brand = brand
print("Welcome use HuiPu computer")
class HuaShuo(Computer):
def __init__(self,brand):
print("----------HUASHUO-----------")
Computer.__init__(self)
self.brand = brand
print("Welcome use HuaShuo computer")
apple_c = Apple("苹果")
huipu_c = HuiPu("惠普")
huashuo_c = HuaShuo("华硕")
''' 运行结果
----------APPLE------------
Computer init...
CPU init...
GPU init...
Power init...
Memory init...
Screen init...
Mouse init...
Keyboard init...
Computer init compulete/finish
Welcome use Apple computer
----------HUIPU------------
Computer init...
CPU init...
GPU init...
Power init...
Memory init...
Screen init...
Mouse init...
Keyboard init...
Computer init compulete/finish
Welcome use HuiPu computer
----------HUASHUO-----------
Computer init...
CPU init...
GPU init...
Power init...
Memory init...
Screen init...
Mouse init...
Keyboard init...
Computer init compulete/finish
Welcome use HuaShuo computer
'''
注:虽然组合与继承可以相互取代,但能够在合适的条件选用合适的关系能够极大的提高开发质量和效率
2.设计模式
(1)工厂模式
将创建对象的过程集合到一起,方便管理和维护
以造车场为例,可造特斯拉、比亚迪、宝马、奔驰等
class CarFactory(object):
def __init__(self,brands):
self.brands = brands
def product(self,brand):
if brand in self.brands:
if brand == "TSL":
return Tsl()
if brand == "BYD":
return Byd()
if brand == "BMW":
return Bmw()
if brand == "BZ":
return Bz()
else:
print("我们不生产此类车型")
return None
class Tsl(object):
def __init__(self):
print("product TSL")
class Byd(object):
def __init__(self):
print("product BYD")
class Bmw(object):
def __init__(self):
print("product BMW")
class Bz(object):
def __init__(self):
print("product BZ")
factory = CarFactory(set(["TSL","BYD","BMW","BZ"]))
byd = factory.product("BYD")
tsl = factory.product("TSL")
(2)单例模式
确保一个类对象只能创建一个实例对象,有助于节约资源、提升性能
以造车场为例,将上面的造车场改为单例模式
class CarFactory(object):
__c_factory = None
__cf_flag = True
def __new__(cls,brands):
if cls.__c_factory == None:
print("new")
cls.__c_factory = object.__new__(cls)
return cls.__c_factory
def __init__(self,brands):
if CarFactory.__cf_flag:
print("init")
self.brands = brands
CarFactory.__cf_flag = False
def product(self,brand):
if brand in self.brands:
if brand == "TSL":
return Tsl()
if brand == "BYD":
return Byd()
if brand == "BMW":
return Bmw()
if brand == "BZ":
return Bz()
else:
print("我们不生产此类车型")
return None
class Tsl(object):
def __init__(self):
print("product TSL")
class Byd(object):
def __init__(self):
print("product BYD")
class Bmw(object):
def __init__(self):
print("product BMW")
class Bz(object):
def __init__(self):
print("product BZ")
factory1 = CarFactory(set(["TSL","BYD","BMW","BZ"]))
factory2 = CarFactory(set(["TSL","BYD","BMW","BZ"]))
print("factory1_id:",id(factory1))
print("factory2_id:",id(factory2))
'''运行结果
new
init
factory1_id: 1911060327448
factory2_id: 1911060327448
'''