码迷,mamicode.com
首页 > 其他好文 > 详细

经典类与新式类的继承顺序

时间:2018-05-07 20:57:40      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:doc   enc   ast   span   oct   har   nic   %s   self   

 1 #经典类与新式类的继承顺序
 2 
 3 class Teacher(object):
 4     cn=Harvard
 5     def __init__(self,name,age,sex):
 6         self.name=name
 7         self.age=age
 8         self.sex=sex
 9         self.__money=5600
10 
11     def show_private_attribule(self):
12         print(private attribute:%s %self.__money)
13 
14 class Master(Teacher):
15     def __init__(self,name,age,sex,subject):
16         Teacher.__init__(self,name,age,sex)#重构父类的构造函数,继承多个就多写一个
17         #super(Master,self).__init__(name,age,sex) #通用写法,继承多个父类都可以用这一个
18                                                 #继承多个类,只运行第一个类的构造函数,其它的不运行
19                                                 #没有,横向逐个找,向上找(查找策略:广度优先 )(新式类)
20                                                 #python2.x是深度优先 (经典类)
21         self.subject=subject
22         print(in the Master)
23 
24     def speak(self):
25         print(I am master %s I study %s subject %(self.name,self.subject))
26 
27 class Doctor(Teacher):
28     def __init__(self,name,age,sex,allowance):
29         Teacher.__init__(self, name, age, sex)
30         #super(Doctor,self).__init__(name,age,sex)
31         self.allowance=allowance
32         print(in the Doctor)
33 
34     def ralation(self,obj):
35         print(%s is %s \‘s student %(obj.name,self.name))
36 
37 class Specialist(Master,Doctor):
38     def __init__(self,name,age,sex,subject,allowance,research_field):#先查找Master构造执行,没有向Doctor查找,再没有向上Teacher查找
39         Master.__init__(self,name,age,sex,subject)
40         Doctor.__init__(self,name,age,sex,allowance)
41         self.research_field=research_field
42         print(in the Specialist)
43 
44 t1=Teacher(Jack,26,M)
45 m1=Master(Alice,22,F,French)
46 d1=Doctor(Monica,28,F,6600)
47 s1=Specialist(Jones,32,F,French,6600,language)

 

经典类与新式类的继承顺序

标签:doc   enc   ast   span   oct   har   nic   %s   self   

原文地址:https://www.cnblogs.com/zhong2018/p/9004686.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!