多态:不同的子类对象,调用相同的父类方法,产生不同的执行结果
class Soldier():
def attack():
def back():
class Army(Soldier):
def attack(self):
print("拼刺刀")
def back(self):
print("撒腿就跑")
class Navy(Soldier):
def attack(self):
print("扔鱼叉")
def back(self):
print("直接跳海")
class AirForce(Soldier):
def attack(self):
print("炮轰")
def back(self):
print("直接跳伞")
obj_army = Army()
obj_navy = Navy()
obj_airforce = AirForce()
lst = [obj_army,obj_navy,obj_airforce]
sign = True
while sign:
num = input("将军请下令")
for i in lst:
if num = "1":
i.attack()
elif num == "2":
i.back()
elif num == "3":
if isinstance(i,AirForce):
i.attack()
else:
i.back()
elif num.upper() == "Q":
print("将军,您辛苦了")
sign = False
break
else:
print("对不起,无法识别")
break
<---------------------------------------------------------------------------->
小人射击小程序
把对象作为程序的最小单元,让对象去操作一切
class BulletBox():
def __init__(self,bulletcount):
self.bulletcount = bulletcount
class Gun():
def __init__(self,bulletbox):
self.bulletbox = bulletbox
def shoot(self,shootcount):
if self.bulletbox.bulletcount <= shootcount:
print("对不起,子弹数量不够,请上子弹")
else:
self.bulletbox.bulletcount -= shootcount
print("答"*shootcount,"还剩下{}子弹".format(self.bulletbox.bulletcount))
class Person():
def __init__(self,gun):
self.gun = gun
def fire(self,firecount):
self.gun.shoot(firecount)
def addcount(self,num):
self.gun.bulletbox.bulletcount += num
danjia = BulletBox(20)
jiatelin = Gun(danjia)
xiaolin = Person(jiatelin)
xiaolin.fire(10)
xiaolin.addcount(10)
xiaolin.fire(100)
<--------------------------------------------------------------------------->
'''
触发时机:实例化类生成对象的时候触发(触发时机在__init__之前)
功能:控制对象的创建过程
参数:至少一个cls接受当前的类,其他根据情况决定
返回值:通常返回对象或None
'''
1.基本语法
class Car():
a = 1
obj = Car()
class Car2(object):
def __new__(cls):
obj = object.__new__(cls)
return obj
obj = Car2()
print(obj)
2.触发时机在__init__之前
class Box():
def __init__(self):
print(2)
def __new__(cls):
print(1)
return object.__new__(cls)
obj = Box()
3.new方法的参数要和init方法参数一一对应
class Box():
def __init__(self,name):
self.name = name
print(2)
def __new__(cls,name):
print(1)
return object.__new__(cls)
obj = Box("王振")
print(obj.name)
<--------------------------------------------------------------------------->
目的就是为了节省内存空间
不需要额外给该对象添加任何成员,这个场景,使用单态
比如:操作数据库的增删改查这样的类,是不需要的
class Singleton():
__obj = None
def __new__(cls):
if cls.__obj is None:
cls.__obj = object.__new__(cls)
return cls.__obj
obj1 = Singleton()
print(obj1)
obj2 = Singleton()
print(obj2)
obj3 = Singleton()
print(obj3)
obj4 = Singleton()
print(obj4)
"""<__main__.Singleton object at 0x000001FB3F207278>
第一次实例化时,if cls.__obj is None 条件为真 , 创建一个对象放到cls.__obj , 最后返回
第二次实例化时,if cls.__obj is None 条件为假 , 直接返回
第三次实例化时,if cls.__obj is None 条件为假 , 直接返回
第三次实例化时,if cls.__obj is None 条件为假 , 直接返回
"""
有这个对象就直接返回,没有对象的话,给你创建一个,保证一个
class Singleton():
__obj = None
def __new__(cls,*args,**kwargs):
if cls.__obj is None:
cls.__obj = object.__new__(cls)
return cls.__obj
def __init__(self,name):
self.name = name
obj1 = Singleton("王振")
obj2 = Singleton("刘伟")
print(obj1.name)
print(obj2.name)
结果都是 刘伟
分析:打印的时候,类还是同一个类,name属性也是同一个属性,只是把name属性里面的值给改了,刘伟覆盖了王振
所以两次打印的都是显示刘伟