面向对象-继承、重用性
class Parentclass1: pass class ParentClass2: pass class SubClass1(Parentclass1): # 单继承 pass class subClass2(Parentclass1, ParentClass2): # 多继承 pass
继承:子类继承父类,可以分为单继承,与多继承。子类继承父类,可以减少重复代码。
class Hero: def __init__(self,nickname,life_value,aggresivity): self.nickname=nickname self.life_value=life_value self.aggresivity=aggresivity def attack(self,enemy): enemy.life_value -= self.aggresivity class Garen(Hero): pass g1 = Garen('d',29,30) print(g1.__dict__) # print(g1.x) # 先在对象这边找,再在对象类找,再在父类找 #属性查找 先在对象这边找,再在对象类找,再在父类找! class Foo: def f1(self): print('from Foo.f1') def f2(self): print('from Foo.f2') self.f1() class Bar(Foo): def f1(self): print('from Bar.f2') b = Bar() b.f2()