python-面向对象设计方法

面向对象
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关系,即一个对象是另一个对象,对象都具备相同的基本属性和行为
	举个例子 苹果电脑是电脑,惠普电脑也是电脑,华硕电脑也是电脑,他们都具备电脑的基本属性和行为
#———————————————————————————————— 代码演示————————————————————————————————————————#
#为方便演示这里的Compputer类用的是前面组合中定义的
class Apple(Computer):  # is-a 关系
	def __init__(self,brand):
		print("----------APPLE------------")
		Computer.__init__(self)
		self.brand = brand
		print("Welcome use Apple computer")

class HuiPu(Computer):  # is-a 关系
	def __init__(self,brand):
		print("----------HUIPU------------")
		Computer.__init__(self)
		self.brand = brand
		print("Welcome use HuiPu computer")

class HuaShuo(Computer):  # is-a 关系
	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")
# 运行结果
#		product BYD
#		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
'''
#--------------------------------------------------------------------------------#
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值