最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

Python继承的代码示例

来源:动视网 责编:小采 时间:2020-11-27 14:20:27
文档

Python继承的代码示例

Python继承的代码示例:本篇文章给大家带来的内容是关于Python继承的代码示例,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。#单继承 class Person(object): def __init__(self,name,age,height,weight): self.name = name self
推荐度:
导读Python继承的代码示例:本篇文章给大家带来的内容是关于Python继承的代码示例,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。#单继承 class Person(object): def __init__(self,name,age,height,weight): self.name = name self


本篇文章给大家带来的内容是关于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 Person
class 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 Student
stu = 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 Father
from mother import Mother
class Child(Father,Mother):
 def __init__(self,money,facevalue):
 Father.__init__(self,money)
 Mother.__init__(self,facevalue)
 def study(self):
 print("studing")
 
 from child import Child
def main():
 ch = Child(5,"NICE")
 print(ch.money,ch.facevalue)
if __name__=='__main__':
 main()

文档

Python继承的代码示例

Python继承的代码示例:本篇文章给大家带来的内容是关于Python继承的代码示例,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。#单继承 class Person(object): def __init__(self,name,age,height,weight): self.name = name self
推荐度:
标签: 例子 的代码 实例
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top