# 父类中没有的属性,在子类中出现,叫做派生属性
# 父类中没有的方法,在子类中出现,叫做派生方法
# 只要是子类的对象调用,子类中有的名字一定用子类的,子类中没有,则找父类,再没有的话报错
class Animal:
def __init__(self,name,attack,hp):
self.name = name
self.attack = attack
self.hp = hp
def eat(self): # 派生方法
print('hp+++')
self.hp += 1000
class Person(Animal):
def __init__(self,name,attack,hp,types):
Animal.__init__(self,name,attack,hp)
self.types = types # 派生属性
class Cat(Animal):
def __init__(self,name,attack,hp,lv,tooth):
Animal.__init__(self,name,attack,hp)
self.lv = lv # 派生属性
self.tooth = tooth
def eat(self):
Animal.eat(self) # 既想使用自己的eat 还要使用父类的eat
# 可以在自己的 eat 中调用 父类的 eat
self.tooth += 2
p = Person('q',22,100,'tree')
c = Cat('mimi',11,300,'monster',4)
c.eat()
print(c.hp)
print(c.tooth)