- #Author:ZhangKanghui
- class School(object):
- def __init__(self,name,addr):
- self.name = name
- self.addr = addr
- self.students = []
- self.staffs = []
- def enroll(self,stu_obj):
- print("welcome %s to shcool" % stu_obj.name)
- self.students.append(stu_obj)
- def hire(self,staff_obj):
- print("welcome %s to shcool" % 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
- def info(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 info(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 = grade
- def info(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_fee(self,amount):
- print("%s had paid fee for $[%s]"%(self.name,amount))
- school = School("清北","北平")
- t1 =Teacher("Sol",30,"Boy",10000,"English")
- t2 =Teacher("Crystal_Lan",30,"Girl",10000,"English")
- s1 =Student("Kanghui",20,"Boy",'001',"Python")
- s2 =Student("Ruixin",30,"Boy",'002',"Yixue")
- t1.info()
- s1.info()
- 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_fee(5000)
来源: https://www.cnblogs.com/zhangkanghui/p/9510287.html