本篇文章给大家带来的内容是关于python继承的代码示例,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
#单继承class person(object): def __init__(self,name,age,height,weight): self.name = name self.age = age self.height = height self.weight = weight def eat(self): print("eating") def walk(self): print("walking") def __str__(self): return "name:%s,age:%d"%(self.name,self.age)from person import personclass student(person): def __init__(self,name,age,height,weight): #调用父类中的属性 super(student,self).__init__(name,age,height,weight) def studey(self): print("studying")from student import studentstu = student("tom",25,252,63)print(stu.name)
#多继承注意,当self.money = money编程私有属性时,即self.__money会出现报错现象,说明私有属性不能直接继承class father(object): def __init__(self,money): self.money = money def eat (self): print("eating") class mother(object): def __init__(self,facevalue): self.facevalue = facevalue def sleep(self): print("slepping") from father import fatherfrom mother import motherclass child(father,mother): def __init__(self,money,facevalue): father.__init__(self,money) mother.__init__(self,facevalue) def study(self): print("studing") from child import childdef main(): ch = child(5,"nice") print(ch.money,ch.facevalue)if __name__=='__main__': main()
以上就是python继承的代码示例的详细内容。
