python学习,day6 继承(学校实例)

# coding=utf-8
# Author: RyAn Bi
class School(object):
    def __init__(self,name,addr):
        self.name = name
        self.addr = addr
        self.students= []
        self.teachers= []
    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.teachers.append(staff_obj)
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):#继承上面的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 a teaching course %s'%(self.name,self.course))


class Student(SchoolMember):
    def __init__(self,name,age,sex,stuid,grade):
        super(Student, self).__init__(name,age,sex)  #父类重构
        self.stuid = stuid
        self.grade = grade
    def tell(self):
        print('''
        _____info of teacher:%s______
        Name:%s
        Age:%s
        Sex:%s
        Stuid:%s
        Grade:%s
        '''%(self.name,self.name,self.age,self.sex,self.stuid,self.grade))
    def pay_tuition(self,amount):
        print('%s has pad tution for amount %s'%(self.name,amount))

school = School('telecom','jinshui')
t1 = Teacher('bi','32','M','2000000','python')
t2 = Teacher('ryan','33','F','4000000','pythondev')
s1 = Student('li','22','M','007','python')
s2 = Student('ji','21','F','005','pythondev')

t1.tell()
school.hire(t1)
s1.tell()
school.enroll(s1)
school.enroll(s2)
print(school.students)
print(school.teachers)
school.teachers[0].teach()
school.students[0].pay_tuition(5000)
for x in school.students:
运行结果
_____info of teacher:bi______ Name:bi Age:32 Sex:M Salary:2000000 Course:python 为新员工bi办理入职手续 _____info of teacher:li______ Name:li Age:22 Sex:M Stuid:007 Grade:python 为学员li办理入学手续 为学员ji办理入学手续 [<__main__.Student object at 0x00000000028FEE48>, <__main__.Student object at 0x00000000028FEE80>] [<__main__.Teacher object at 0x00000000028FEDD8>] bi is a teaching course python li has pad tution for amount 5000 li has pad tution for amount 3000 ji has pad tution for amount 3000

 

    x.pay_tuition(3000)
posted @ 2020-01-13 14:52  bbgoal  阅读(212)  评论(0编辑  收藏  举报