学习笔记,类,士兵突击
class Gun(): def __init__(self, gun_name, bullet=0): self.gun_name = gun_name self.bullet = bullet def __str__(self): return "[%s]子弹数[%d]" % (self.gun_name, self.bullet) def add_bullet(self, bullet): self.bullet += bullet def shoot(self): if self.bullet <= 0: print("没有子弹了") return self.bullet -= 1 print("piu,让子弹飞一会") print("剩余子弹%d发" % self.bullet) class Person(): def __init__(self, name): self.name = name self.gun = "赤手空拳" def __str__(self): return "%s现在的武器是[%s]" % (self.name, self.gun) def fire(self): if self.gun == "赤手空拳": print("没枪呀,打不过") return self.gun.add_bullet(20) print("装填子弹,干他") self.gun.shoot() ak = Gun("AK47") xsd = Person("许三多") print(xsd) xsd.fire() xsd.gun = ak print(xsd) xsd.fire()