# 1 人狗大战的简单版
# class Person:
# role = 'person'
# def __init__(self,name,aggressivity,life_value):
# self.name = name
# self.aggressivity = aggressivity
# self.life_value = life_value
# def attack(self,do):
# print('%s攻击了%s'%(self.name,do.name))
# do.life_value -= self.aggressivity
# class Dog:
# role = 'dog'
# def __init__(self, name, aggressivity, life_value):
# self.name = name
# self.aggressivity = aggressivity
# self.life_value = life_value
# def bite(self, per):
# print('%s咬了%s' % (self.name,per.name))
# per.life_value -= self.aggressivity
# hero = Person('王英雄',120,400)
# Two_dog = Dog('大阿黄',150,400)
# hero.attack(Two_dog)
# print(Two_dog.life_value)
# Two_dog.bite(hero)
# print(hero.life_value)
# 2 运用组合写 内容人加了打狗棒 攻击力100,狗加了狗套防御力80,

# class Person:
# role = 'person'
# def __init__(self,name,aggressivity,life_value,money):
# self.name = name
# self.aggressivity = aggressivity
# self.life_value = life_value
# self.money = money
# def attack(self,do):
# print('%s攻击了%s'%(self.name,do.name))
# do.life_value -= self.aggressivity
# def pick_weapon(self,dagougun):
# if self.money > dagougun.price:
# self.money -= dagougun.price
# self.dagougun = Weapon #这是最主要的关键点也是联系点 Person 以另一个的Weapson 的对象为属性
# self.aggressivity += dagougun.aggressivity
# class Dog:
# role = 'dog'
# def __init__(self, name, aggressivity, life_value):
# self.name = name
# self.aggressivity = aggressivity
# self.life_value = life_value
# def bite(self, per):
# print('%s咬了%s' % (self.name,per.name))
# per.life_value -= self.aggressivity
# class Weapon:
# def __init__(self,name,aggressivity,price):
# self.name = name
# self.aggressivity = aggressivity
# self.price = price
# hero = Person('王英雄',120,400,10)
# Two_dog = Dog('大阿黄',150,400)
# dgg = Weapon('打狗棍',80,8)
# hero.pick_weapon(dgg)
# hero.attack(Two_dog)
# print(Two_dog.life_value)
# Two_dog.bite(hero)
# print(hero.life_value)

# 3 用继承的方法进行改造

class Animal:
def __init__(self,name,aggressivity,life_value):
self.name = name
self.aggressivity = aggressivity
self.life_value = life_value
def eat(self):
self.aggressivity += 10
print('幸运大礼包攻击力加10')
class Person(Animal):
role = 'person'
def __init__(self,name,aggressivity,life_value,money):
# Animal.__init__(self,name,aggressivity,life_value) #第一种经典派生属性
super().__init__(name,aggressivity,life_value) #第二种新式 派生属性 比前者少个self
self.money = money
def attack(self,do):
print('%s攻击了%s'%(self.name,do.name))
do.life_value -= self.aggressivity
def pick_weapon(self,dagougun):
if self.money > dagougun.price:
self.money -= dagougun.price
self.dagougun = Weapon #这是最主要的关键点也是联系点 Person 以另一个的Weapson 的对象为属性
self.aggressivity += dagougun.aggressivity
def eat(self):
# Animal.eat(self)
super().eat()
print('%s吃了一顿大餐,增加50攻击力' % self.name)
self.aggressivity += 30
class Dog(Animal):
role = 'dog'
def __init__(self, name, aggressivity, life_value):
Animal.__init__(self, name, aggressivity, life_value)
def bite(self, per):
print('%s咬了%s' % (self.name,per.name))
per.life_value -= self.aggressivity
def eat(self):
# Animal.eat(self)
print('%s吃了一根骨头,增加50攻击力'% self.name)
self.aggressivity += 50
class Weapon:
def __init__(self,name,aggressivity,price):
self.name = name
self.aggressivity = aggressivity
self.price = price

# super(Dog,Two_dog).eat()
# Animal.eat()
hero = Person('王英雄',120,400,10)
Two_dog = Dog('大阿黄',150,400)
dgg = Weapon('打狗棍',80,8)
# hero.pick_weapon(dgg)
# hero.eat() # 调用person的 30点攻击力 另外person自己还继承了父类的10点
# hero.attack(Two_dog)
# print(Two_dog.life_value)
# ---------
# Animal.eat(Two_dog) #类名.方法(对象) 继承父类
# super(Dog,Two_dog).eat() #super(子类名,对象名) 继承父类
Two_dog.eat()
# print(hero.aggressivity)
Two_dog.bite(hero)
print(hero.life_value)