• Python_Example_多子类继承程序


    2018-09-12

    Author: 楚格

    IDE: Pycharm2018.02   Python 3.7   

    KeyWord :  继承

    Explain:

    class A:
    def __init__(self):
    print("A")
    class B(A):
    pass
    # def __init__(self):
    # print("B")
    class C(A):
    pass
    # def __init__(self):
    # print("C")
    class D(B,C):
    pass
    # def __init__(self):
    # print("D")


    obj = D()

    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 0x0000000002995048>, <__main__.Student object at 0x0000000002995080>]
    school.staffs   :  [<__main__.Teacher object at 0x0000000002980F98>]
    帕萨特 is teaching course [freeRTOS]
    远景 has paid tution for $5000
    <__main__.Student object at 0x0000000002995048>
    [<__main__.Student object at 0x0000000002995048>, <__main__.Student object at 0x0000000002995080>]
    悦动 has paid tution for $5000
    <__main__.Student object at 0x0000000002995080>
    [<__main__.Student object at 0x0000000002995048>, <__main__.Student object at 0x0000000002995080>]
    
    Process finished with exit code 0
    

      ---

    额外多态

    class Animal:
        def __init__(self, name):  # Constructor of the class
            self.name = name
    
        def talk(self):  # Abstract method, defined by convention only
            pass #raise NotImplementedError("Subclass must implement abstract method")
    
        @staticmethod
        def animal_talk(obj):
            obj.talk()
    
    class Cat(Animal):
        def talk(self):
            print('Meow!')
    
    
    class Dog(Animal):
        def talk(self):
            print('Woof! Woof!')
    
    
    d = Dog("科沃兹")
    #d.talk()
    
    c = Cat("科鲁兹")
    #c.talk()
    #
    # def animal_talk(obj):
    #     obj.talk()
    
    Animal.animal_talk(c)
    Animal.animal_talk(d)
  • 相关阅读:
    如何找bug
    信号量
    带组装的测试
    Oracle的一些操作
    NPOI的操作
    初次认识 C# win32 api
    C# 通过Selecnuim WebDriver操作非IE浏览器
    DevExpress.chartControt画趋势图
    DevExpress.chartContro控件保存图片和打印图片
    SqlServer基础复习
  • 原文地址:https://www.cnblogs.com/caochucheng/p/9637501.html
Copyright © 2020-2023  润新知