zoukankan      html  css  js  c++  java
  • python_98_面向对象_学校

    class School(object):#以后都加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)
            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('Hebut','天津')
    t1=Teacher('GMM',30,'woman',200000,'BCI')
    t2=Teacher('XGZ',53,'woman',300000,'EEG')
    s1=Student('QZG','25','male',1001,'BCI')
    s2=Student('CU','26','female',1002,'EEG')
    
    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(18000)
    

      参考:http://www.cnblogs.com/alex3714/articles/5188179.html

  • 相关阅读:
    线程、同步
    异常、线程
    Map
    List、Set
    Collection、泛型
    Object类、常用API
    h5-上传图片预览
    js 获取get参数
    《转》完美解决微信video视频隐藏控件和内联播放问题
    写入文件(覆盖文件的内容)
  • 原文地址:https://www.cnblogs.com/tianqizhi/p/8471195.html
Copyright © 2011-2022 走看看