类的继承可分成多层,但是父类只有一个。子类是可以有多个。一下是演示代码: class A(object):#父类 def init(self): self.money = 10 def cost(self): self.money -= 1 class B(A):#子类 pass b = B() print(b.money) b.cost() print(b.money) #demo
class Master(object): def init(self, name):#创建类的时候会自动init方法 self.name = name self.kongfu = “科比的单打技术”
def ball_Kill(self): print("{}学会{}来单打姚明".format(self.name, self.kongfu)) def dayandai(self): print("学会科比的抽烟的烟袋")class China(object): def init(self, name): self.name = name self.kongfu = “周琦的单打技术”
def ball_Kill(self): print("{}学会{}的技术吊打科比".format(self.name, self.kongfu)) def smoke(self): print("学周琦排队买煎饼果子")class Student(Master,China): def init(self, name): self.__money = 10 #加上__可以变成私有化 self.name = name self.kongfu = “自己的单打技术”
def ball_Kill(self): print("{}学会了无敌的技术单打女盆友".format(self.name)) def ball_K_Kill(self): Master.__init__(self,self.name) Master.ball_Kill(self) def ball_Z_Kill(self): China.__init__(self,self.name) China.ball_Kill(self) #在类的内部使用属性 def cost(self): self.__money -= 1 print("建立学校,花掉一个亿,还剩{}亿".format(self.__money))class PrenticePrentice(Student): pass dongdong = Student(“东东”) dongdong.ball_Kill() dongdong.dayandai() dongdong.ball_Kill() dongdong.smoke() dongdong.cost()
#mro 方法 #print(Student.mro()) print(Student.mro) dongdong.ball_Kill() dongdong.ball_K_Kill() dongdong.ball_Z_Kill()
print("===mysan=") my_child = PrenticePrentice(“小东”)
my_child.ball_Kill() my_child.ball_K_Kill() my_child.ball_Z_Kill() print(my_child.money)#这里的money是父类将money进行私有化。