一个多继承的Python实例

一个多继承的Python实例

#!/user/bin/env python
# -*- coding: utf-8 -*-

class School(object):
    def __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 SchoolMenber(object):
    def __init__(self,name,age,sex):
        self.name=name
        self.age=age
        self.sex=sex

    def tell(self):
        pass

class Teacher(SchoolMenber):
    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(SchoolMenber):
    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 teacher:%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 tuition for $%s' %(self.name,amount))


School=School('老男孩IT','沙河')
t1=Teacher("oldboy",56,'M',2000000,'linux')
t2=Teacher("alex",22,'M',3000,'pythonops')
s1=Student("chenhua",36,'M',1001,'pythonops')
s2=Student("xuwei",19,'F',1002,'linux')

t1.tell()
s1.tell()
School.hire(t1)
School.enroll(s1)
School.enroll(s2)
print(School.students)
print(School.staffs)
for stu_name in School.students:
    print(stu_name.name)
School.staffs[0].teach()

for stu in School.students:
    stu.pay_tuition(5000)

你可能感兴趣的:(python,python)