• 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)
  • 相关阅读:
    搬运好文章->>>>>子网划分详解
    搬运好文章->>>>>子网掩码详解
    搬运好文章->>>>>IP地址和MAC地址详解
    搬运好文章->>>>>计算机中进制之间的关系和转换
    extend 与 append 的区别
    数据类型---字符串
    多引号的作用,字符串格式化
    列表复制的几种方法
    十六进制和二进制转换
    python奇偶数求和
  • 原文地址:https://www.cnblogs.com/davidz/p/9856459.html
Copyright © 2020-2023  润新知