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)
  • 相关阅读:
    一个好用的H5tab切换(抽屉功能)
    jQuery的ajax请求
    原生js的ajax请求
    微信小程序封装get、post请求
    微信小程序 data数据的赋值和取值
    gitLab更新文件命令
    vue中localStorage的使用
    linux就该这么学--课程第15天
    linux就该这么学--课程第14天
    linux就该这么学--课程第13天
  • 原文地址:https://www.cnblogs.com/davidz/p/9856459.html
Copyright © 2011-2022 走看看