python记录 · 2017年11月17日

python(继承演示)

class School(object):            #学校父类
    def __init__(self,name,addr):
        self.name = name
        self.addr = addr
        self.students =[]
        self.staffs =[]
    def enroll(self,stu_obj):
        print("为学员%s 办理注册手续"%stu_obj.name )
        self.students.append(stu_obj)
    def hire(self,staff_obj):
        self.staffs.append(staff_obj)
    def hire(self,staff_obj):
        self.staffs.append(staff_obj)
        print("雇佣新员工%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 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 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("Sooele学校","cooi")
t1 = Teacher("Sooele老师",22,"Men",112200,"Linux")
t2 = Teacher("Alex",22,"M",3000,"PythonDevOps")
s1 = Student("Sooele1111",22,"M",1001,"PythonDevOps")
s2 = Student("Cooi1111",19,"M",1002,"Linux")
t1.tell()
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(5000)