Python_Example_多子类继承程序

2021-07-01 11:06

阅读:385

标签:ber   proc   nis   course   linu   school   taf   注册   app   

2018-09-12

Author: 楚格

IDE: Pycharm2018.02   Python 3.7   

KeyWord :  继承

Explain:

 

1------------------------------------------------------------------------------------------------------------------

--

    # 定义学校
class School(object):
    print(class School(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):
    print(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):
    print(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):
    print(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("帕萨特", 2018,"MF",2000,"freeRTOS")
t2 = Teacher("探索者", 2019,"MH",3000,"Ucos-II")

s1 = Student("远景",25,"MF",1001,"python")
s2 = Student("悦动",28,"MH",1002,"Linux")


t1.Tell()
s1.Tell()

school.Hire(t1)
school.Enroll(s1)
school.Enroll(s2)

print(school.students : ,school.students)
print(school.staffs   : ,school.staffs)

school.staffs[0].teach()


for stu in school.students:
    stu.Pay_Tuition(5000)
    print(stu)
    print(school.students)

 

--

 RUN Result

--

---------------------------
class School(object):
class SchoolMember(object):
class Teacher(SchoolMember):
class Student(SchoolMember):

        ---- info of Teacher:帕萨特 ----
            Name   :帕萨特
            Age    :2018
            Sex    :MF
            Salary :2000
            Course :freeRTOS
        

        ---- info of Student:远景 ----
            Name   :远景
            Age    :25
            Sex    :MF
            Stu_id :1001
            Grade  :python
        
雇佣新员工帕萨特
学生远景 注册办理
学生悦动 注册办理
school.students :  [<__main__.student object at>, <__main__.student object at>]
school.staffs   :  [<__main__.teacher object at>]
帕萨特 is teaching course [freeRTOS]
远景 has paid tution for $5000
<__main__.student object at>
[<__main__.student object at>, <__main__.student object at>]
悦动 has paid tution for $5000
<__main__.student object at>
[<__main__.student object at>, <__main__.student object at>]

Process finished with exit code 0

  

Python_Example_多子类继承程序

标签:ber   proc   nis   course   linu   school   taf   注册   app   

原文地址:https://www.cnblogs.com/caochucheng/p/9637501.html


评论


亲,登录后才可以留言!