# Author kevin_hou class School(object): def __init__(self,name,addr): self.name = name self.addr = addr self.students = [] self.staffs = [] def enroll(self,stu_obj): self.students.append(stu_obj) print("---------Prepare for student %s enrolling--------"%stu_obj.name) def hire(self,staff_obj): self.staffs.append(staff_obj) print("---------hire a new staff %s--------"%staff_obj.name) 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): 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 Teachers: %s------------------- Name: %s Age: %s Sex: %s Salary: %s Course: %s ‘‘‘%(self.name,self.name,self.age,self.sex,self.salary,self.course)) def tech(self): print("%s is teasching 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 = grade def tell(self): print( ‘‘‘ ----------------info of Students: %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_tution(self,amount): print("%s has paid tution for $s"%(self.name,amount)) school = School("Kevin","Shanghai") t1 = Teacher("Alex", 22, "M", 1002,"python3") t2 = Teacher("Jane", 33,"F",800,"C language") s1 = Student("Curry", 22, "M", 1002,"python3") s2 = Student("Rossal", 33,"M",800,"C language") t1.tell() # ----------------info # of # Teachers: Alex - ------------------ # Name: Alex # Age: 22 # Sex: M # Salary: 1002 # Course: python3 s1.tell() # ----------------info # of # Students: Curry - ------------------ # Name: Curry # Age: 22 # Sex: M # Stu_id: 1002 # Grade: python3 school.hire(s1) #---------hire a new staff Curry-------- school.enroll(s1) #---------Prepare for student Curry enrolling-------- school.enroll(s2) #---------Prepare for student Rossal enrolling-------- print(school.students) #[<__main__.Student object at 0x0144CC70>, <__main__.Student object at 0x0144CCB0>] print(school.staffs) #[<__main__.Student object at 0x0144CC70>]
原文:https://www.cnblogs.com/kevin-hou1991/p/13583566.html