我们用类的继承来做一个学校的程序。
里面包含类--school
类--schoolmermber
子类--teacher
子类--students
# __*__ coding: utf-8 __*__# __author__ = "David.z"class School(object): members = 0def __init__(self,name,addr): self.name = name self.addr = addr self.students = []# self.teachers = []self.staffs = []def enroll(self,stu_obj):print ("为学员%s办理注册手续"%stu_obj.name) self.students.append(stu_obj)def hire(self,staff_obj):print ("为员工%s办理入职手续" % staff_obj.name) self.staffs.append(staff_obj)class SchoolMember(object):def __init__(self,name,age,sex): self.name = name self.age = age self.sex =sex# passdef tell(self):passclass Teacher(SchoolMember):def __init__(self,name,age,sex,salary,course): super(Teacher, self).__init__(name,age,sex) self.salary = salary self.course = coursedef 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):def __init__(self,name,age,sex,stu_id,grade): super(Student, self).__init__(name,age,sex) self.stu_id = stu_id self.grade = gradedef 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("薛永春",26,"男",200000,"Cisco") t2 = Teacher("陈洁",22,"男",180000,"sql") s1 = Student("周倩",18,"女",1001,"Python") s2 = Student("姚景鹏",18,"男",1002,"Cisco") t1.tell() t1.teach() s1.tell() school.hire(t1) school.enroll(s1) school.enroll(s2)print (school.students)print (school.staffs) school.staffs[0].teach()for stu in school.students: stu.pay_tuition(20000)