1 class Monster:
2 def __init__(self, hp=200):
3 self.hp = hp
4
5 def run(self):
6 print('移动到某个位置')
7
8
9 class Animals(Monster): # 定义Monster的一个子类
10 def __init__(self, hp=50):
11 super().__init__(hp) # 继承父类的属性
12
13
14 class Boss(Monster):
15 def __init__(self, hp=1000):
16 super().__init__(hp)
17
18 def run(self): # 与父类的方法重名,那么就只用当前的方法;类似局部变量
19 print('I am the boss, get away!')
20
21
22 m1 = Monster(150)
23 m1.run()
24 print(m1.hp)
25 a1 = Animal(50)
26 a1.run() # 调用父类的方法
27 b1 = Boss(800)
28 print(b1.hp)
29 b1.run()
30 print('m1的类别:', type(m1))
31 print('a1的类别:', type(m1))
32 # 下面判断子类关系
33 print(isinstance(b1, Monster)) # True
34 # 之前学习的数字、字符串、元组等都是类,而是都是object的子类
35 print(type(‘123’))
36 print(isinstance(['1', '2'], object)) # True