封装
class Player: def __init__(self, name, hp): # 属性 self.__name = name # 私有 self.hp = hp # 方法 def print_role(self): print('%s: %d' % (self.__name, self.hp)) def update_name(self, new_name): self.__name = new_nameuser1 = Player('tom', 100)user1.print_role()class Monster: pass # 不要报错# 输出tom: 100
继承/多态
class Monster: def __init__(self, hp=100): self.hp = hp def run(self): print('run') def who_am_i(self): print('fu lei')class Animals(Monster): def __init__(self, hp=11): super().__init__(hp)class Boss(Monster): def __init__(self, hp=1000): super().__init__(hp) def who_am_i(self): print('boss')a = Animals()print(a.hp)a2 = Boss()a2.who_am_i()# 输出11boss
根类