判断类型

  1. class Monster:
  2. def __init__(self, hp=100):
  3. self.hp = hp
  4. def run(self):
  5. print('run')
  6. def who_am_i(self):
  7. print('fu lei')
  8. class Animals(Monster):
  9. def __init__(self, hp=11):
  10. super().__init__(hp)
  11. class Boss(Monster):
  12. def __init__(self, hp=1000):
  13. super().__init__(hp)
  14. def who_am_i(self):
  15. print('boss')
  16. a1 = Monster()
  17. a2 = Animals()
  18. a3 = Boss()
  19. print(type(a1))
  20. print(type(a2))
  21. print(type(a3))

判断继承关系

  1. class Monster:
  2. def __init__(self, hp=100):
  3. self.hp = hp
  4. def run(self):
  5. print('run')
  6. def who_am_i(self):
  7. print('fu lei')
  8. class Animals(Monster):
  9. def __init__(self, hp=11):
  10. super().__init__(hp)
  11. class Boss(Monster):
  12. def __init__(self, hp=1000):
  13. super().__init__(hp)
  14. def who_am_i(self):
  15. print('boss')
  16. a1 = Monster()
  17. a2 = Animals()
  18. a3 = Boss()
  19. print(isinstance(a2, Monster))
  20. # 输出
  21. True