zoukankan      html  css  js  c++  java
  • 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)
  • 相关阅读:
    最新屏蔽微信举报方法
    C# WIN 生成机器码
    Quartz.net的快速简单上手使用以及防止IIS回收停止Job的处理
    MVC、Web API 请求接口报错“自定义错误模块不能识别此错误。”解决
    获取微信短链接的官方接口
    Window 通过cmd查看端口占用、相应进程、杀死进程
    微信域名检测、防封,微信跳转技术揭秘(二) -- 微信跳转揭秘
    微信域名检测、防封,微信跳转技术揭秘(一) -- 域名检测原理及防封方案
    各种比较方便给力的小工具
    《Git学习指南》学习笔记(三)
  • 原文地址:https://www.cnblogs.com/bbgoal/p/12187287.html
Copyright © 2011-2022 走看看