码迷,mamicode.com
首页 > 编程语言 > 详细

python------面向对象介绍之经典类与新式类的继承顺序

时间:2018-09-28 23:01:00      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:student   sch   注册   实例   course   print   新员工   办理   分享   

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

技术分享图片

 

 

 
1
class A: 2 def __init__(self): 3 print("A") 4 5 class B(A): 6 def __init__(self): 7 print("B") 8 9 class C(A): 10 def __init__(self): 11 print("C") 12 13 class D(B,C): 14 pass 15 16 obj = D()

注:python2.x 经典类是按深度优先来继承的,新式类是按广度优先来继承的;

     python3.x  经典类和新式类都是按广度优先继承的。

 

 

二. 继承实例

 

 1 """继承实例"""
 2 class School(object):
 3     def __init__(self,name,addr):
 4         self.name = name
 5         self.addr = addr
 6         self.students = []
 7         self.staffs = []
 8 
 9     def enroll(self,stu_obj):
10         print("为学员%s 办理注册手续"% stu_obj)
11         self.students.append(stu_obj)
12     def hire(self,staff_obj):
13         print("雇佣新员工 %s" % staff_obj)
14         self.staffs.append(staff_obj)
15 
16 
17 
18 
19 class SchoolMember(object):
20     def __init__(self,name,age,sex):
21         self.name = name
22         self.age = age
23         self.sex = sex
24     def tell(self):
25         pass
26 
27 class Teacher(SchoolMember):
28     def __init__(self,name,age,sex,salary,course):
29         super(Teacher,self).__init__(name,age,sex)
30         self.salary = salary
31         self.course = course
32     def tell(self):
33         print(‘‘‘
34         ------info of Teacher : %s----
35         Name :%s
36         Age :%s
37         Sex :%s
38         Salary :%s
39         Course :%s
40         ‘‘‘%(self.name,self.name,self.age,self.sex,self.salary,self.course))
41 
42     def teach(self):
43         print("%s is teaching course[%s]" %(self.name,self.course))
44 
45 class Student(SchoolMember):
46     def __init__(self,name,age,sex,stu_id,grade):
47         super(Student,self).__init__(name,age,sex)
48         self.stu_id = stu_id
49         self.grade = grade
50     def tell(self):
51         print(‘‘‘
52                 ------info of Student : %s----
53                 Name :%s
54                 Age :%s
55                 Sex :%s
56                 Stu_id :%s
57                 Grade :%s
58                 ‘‘‘ % (self.name, self.name, self.age, self.sex, self.stu_id, self.grade))
59     def pay_tuition(self,amount):
60         print("%s has paid tution for $%s" %(self.name,amount))
61 
62 school = School("天字一号","外太空")
63 
64 t1 = Teacher("xiaolaizi",20,"m",20000,"english")
65 t2 = Teacher("zhangsan",18,"m",200,"chinese")
66 
67 s1 = Student("lisi",10,"f",110119,"python")
68 s2 = Student("wangmaiz",8,"f",110118,"C++")
69 
70 t1.tell()
71 s1.tell()
72 school.hire(t1)
73 school.enroll(s1)
74 
75 print(school.students)
76 print(school.staffs)
77 school.staffs[0].teach()
78 
79 for stu in school.students:
80     stu.pay_tuition(3000)

 

python------面向对象介绍之经典类与新式类的继承顺序

标签:student   sch   注册   实例   course   print   新员工   办理   分享   

原文地址:https://www.cnblogs.com/bltstop/p/9716001.html

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