Loading... ### 代码 ```Python class Monster(): #定义怪物类 def __init__(self, hp = 100): self.hp = hp def run(self): print("移动一个位置") def whoami(self): print("我是怪物") class Animals(Monster): #普通类怪物 def __init__(self, hp = 10): super().__init__(hp) #引用父类的定义 class Boss(Monster): #Boss类怪物 def __init__(self, hp=1000): super().__init__(hp) # 引用父类的定义 def whoami(self): print("我是Boss我怕谁") #子类中重名函数会覆盖父类中的定义 a1 = Monster(200) print(a1.hp) a1.run() a2 = Animals(20) print(a2.hp) a2.run() a3 = Boss(1300) print(a3.hp) a3.whoami() #判断从属关系 #type, isinstance print("a1 的类型是: %s" %type(a1)) print("a2 的类型是: %s" %type(a2)) print("a3 的类型是: %s" %type(a3)) #判断a1是否从属于Monster print(isinstance(a1,Monster)) ``` ### 运行   文章名: 《类的继承》 本文链接:https://lula.fun/965.html 除特别注明外,文章均由 噜啦 原创  原创文章 转载时请注明 出处 以及文章链接 Last modification:September 29th, 2019 at 06:14 pm © 允许规范转载 Support 如果觉得我的文章对你有用,请随意赞赏 ×Close Appreciate the author Sweeping payments