zoukankan      html  css  js  c++  java
  • python之类的继承

    我们用类的继承来做一个学校的程序。

    里面包含类--school

    类--schoolmermber

    子类--teacher

    子类--students

    然后实现一些注册,交学费,打印老师和学生信息的功能。

    # __*__ coding: utf-8 __*__
    # __author__ = "David.z"
    
    class School(object):
        members = 0
        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 SchoolMember(object):
        def __init__(self,name,age,sex):
            self.name = name
            self.age = age
            self.sex =sex
        # pass
        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("思远教育学院","湖北武汉")
    
    t1 = Teacher("薛永春",26,"",200000,"Cisco")
    t2 = Teacher("陈洁",22,"",180000,"SQL")
    
    s1 = Student("周倩",18,"",1001,"Python")
    s2 = Student("姚景鹏",18,"",1002,"Cisco")
    t1.tell()
    t1.teach()
    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(20000)
  • 相关阅读:
    u-boot启动流程分析(1)_平台相关部分
    Linux文件系统简介
    PHP将部分内容替换成星号
    自制jQuery焦点图切换简易插件
    一次解决页面特效问题的排查记录
    自制jQuery标签插件
    一套后台管理html模版
    自制jquery可编辑的下拉框
    注册页面的一些记录
    CSS选择器的一些记录
  • 原文地址:https://www.cnblogs.com/davidz/p/9856459.html
Copyright © 2011-2022 走看看