Python_Example_多子类继承程序
Posted caochucheng
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python_Example_多子类继承程序相关的知识,希望对你有一定的参考价值。
2018-09-12
Author: 楚格
IDE: Pycharm2018.02 Python 3.7
KeyWord : 继承
Explain:
1------------------------------------------------------------------------------------------------------------------
--
# 定义学校 class School(object): print(‘class School(object):‘) def __init__(self,name,addr): self.name = name # 名称 self.addr = addr # 地址 self.students =[] self.staffs =[] # 注册方法 def Enroll(self,stu_obj): print("学生%s 注册办理"%stu_obj.name ) self.students.append(stu_obj) # def Hire(self,staff_obj): self.staffs.append(staff_obj) print("雇佣新员工%s" % staff_obj.name) class SchoolMember(object): print(‘class SchoolMember(object):‘) def __init__(self,name,age,sex): self.name = name self.age = age self.sex = sex def Tell(self): pass class Teacher(SchoolMember): print(‘class Teacher(SchoolMember):‘) def __init__(self,name,age,sex,salary,course): super(Teacher,self).__init__(name,age,sex) self.salary = salary self.course = course # 打印自己信息 def Tell(self): print(‘‘‘ ---- info of Teacher:%s ---- Name :%s Age :%s Sex :%s Salary :%s Course :%s ‘‘‘%(self.name,self.name,self.age,self.sex,self.salary,self.course)) def teach(self): print("%s is teaching course [%s]" %(self.name,self.course)) class Student(SchoolMember): print(‘class Student(SchoolMember):‘) def __init__(self,name,age,sex,stu_id,grade): super(Student,self).__init__(name,age,sex) self.stu_id = stu_id self.grade = grade def Tell(self): print(‘‘‘ ---- info of Student:%s ---- Name :%s Age :%s Sex :%s Stu_id :%s Grade :%s ‘‘‘ % (self.name, self.name, self.age, self.sex, self.stu_id, self.grade)) def Pay_Tuition(self,amount): print("%s has paid tution for $%s"% (self.name,amount) ) #================================================= school = School("科沃兹","楚格") t1 = Teacher("帕萨特", 2018,"MF",2000,"freeRTOS") t2 = Teacher("探索者", 2019,"MH",3000,"Ucos-II") s1 = Student("远景",25,"MF",1001,"python") s2 = Student("悦动",28,"MH",1002,"Linux") t1.Tell() s1.Tell() school.Hire(t1) school.Enroll(s1) school.Enroll(s2) print(‘school.students : ‘,school.students) print(‘school.staffs : ‘,school.staffs) school.staffs[0].teach() for stu in school.students: stu.Pay_Tuition(5000) print(stu) print(school.students)
--
RUN Result
--
--------------------------- class School(object): class SchoolMember(object): class Teacher(SchoolMember): class Student(SchoolMember): ---- info of Teacher:帕萨特 ---- Name :帕萨特 Age :2018 Sex :MF Salary :2000 Course :freeRTOS ---- info of Student:远景 ---- Name :远景 Age :25 Sex :MF Stu_id :1001 Grade :python 雇佣新员工帕萨特 学生远景 注册办理 学生悦动 注册办理 school.students : [<__main__.Student object at 0x0000000002995048>, <__main__.Student object at 0x0000000002995080>] school.staffs : [<__main__.Teacher object at 0x0000000002980F98>] 帕萨特 is teaching course [freeRTOS] 远景 has paid tution for $5000 <__main__.Student object at 0x0000000002995048> [<__main__.Student object at 0x0000000002995048>, <__main__.Student object at 0x0000000002995080>] 悦动 has paid tution for $5000 <__main__.Student object at 0x0000000002995080> [<__main__.Student object at 0x0000000002995048>, <__main__.Student object at 0x0000000002995080>] Process finished with exit code 0
以上是关于Python_Example_多子类继承程序的主要内容,如果未能解决你的问题,请参考以下文章
Python--面向对象的程序设计之多继承组合应用开发软件规范