• 类的继承,深度优先于广度优先


    深度优先与广度优先

    #coding=utf-8
    class people(object):
        # def __init__(self):
        #     self.age = 45
        #     print('People')
        def sleep(self):
            print(123)
    
    class Relation(object):
        def __init__(self):
            print('Relation')
    
    class Man(people,Relation):
        def __init__(self):
            super(Man,self).__init__() #这里继承了两个类,但是只会执行people的构造方法,不会执行relation的,如果people没有构造函数就走relation
        def sleep(self):
            #people.sleep(self)
            super(Man,self).sleep()
            print(456)
    
    # a = Man()
    # a.sleep()
    #深度优先于广度优先
    """
    python2中经典类是深度优先,新式类是广度优先,python3中都是广度优先
    """
    class A(object):
        def __init__(self):
            print('A')
    
    class B(A):
        def __init__(self):
            print('B')
    
    class C(A):
        def __init__(self):
            print('C')
    
    class D(B,C):
        # def __init__(self):
        #     pass
        """
        深度优先:若D没有初始化函数,先去找B,B也没有去找A,A也没有再找C
        广度优先:若D没有初始化函数,先去找B,B也没有去找C,C也没有去找A
        """
    

      

    #coding=utf-8
    """
    新式类与旧式类的一个有区别的地方,主要表现在多个类继承上,
    新式类遵循的是广度优先,而旧式类遵循的是深度优先
    python2中经典类按照深度优先继承的,
    pyhton3中,经典类和新式都是按照广度优先继承的
    """
    
    #class people:  #经典类
    class people(object): #新式类
    
        def __init__(self,name,age):
            self.name = name
            self.age = age
    
        def eat (self):
            print '%s is rating'%self.name
    
        def talk(self):
            print '%s is talking'%self.name
    
        def sleep(self):
            print '%s is sleeping'%self.name
    
    class Relation(object):
    
        def make_friends(self,obj):
            print '%s like %s'%(self.name,obj)
    
    class Man(people,Relation):
    
        def __init__(self,name,age,money):
            #people.__init__(self,name,age)
            super(Man,self).__init__(name,age)   #新式类写法
            self.money = money
    
        def play_game(self):
            print '%s is playing game'%self.name
    
        def sleep(self):
            people.sleep(self)    #重写父类方法
            print 'man is sleep'
    
    class Woman(people,Relation):
    
        def song(self):
            print '%s is sing a song'%self.name
    
    man = Man('zq','25','1500')
    man.sleep()
    
    class School(object):
        def __init__(self,name,addr):
            self.name = name
            self.addr = addr
            self.student =[]
            self.teacher =[]
    
        def enroll(self,stuobj):
            print 'service for%s'%stuobj.name
            self.student.append(stuobj)
    
    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):
            super(tell,self).tell()
            print  '''info of teacher:%s' 
                     name: %s
                     age:%s
                     salary:%s
                     course:%s
                   '''%(self.name,self.name,self.age,self.salary,self.course)
    
        def teach(self):
            print '%s is teaching '%self.name
    
    class Student(SchoolMember):
        def __init__(self,name,age,sex,schoolid,course):
            super(Student, self).__init__(name,age,sex)
            self.id = schoolid
            self.course = course
    
        def tell(self):
            print  '''info of student:%s' 
                     name: %s
                     age:%s
                     id:%s
                     course:%s
                   '''%(self.name,self.name,self.age,self.schoolid,self.course)
    
  • 相关阅读:
    requests post一个json数据
    Beautiful Soup常见的解析器
    【bzoj5008】方师傅的房子 计算几何
    【bzoj4842】[Neerc2016]Delight for a Cat 线性规划与网络流
    【bzoj4764】弹飞大爷 LCT
    【bzoj2654】tree 二分+Kruscal
    [NOIP2017]逛公园 最短路+拓扑排序+dp
    【bzoj4165】矩阵 堆+STL-map
    【bzoj3717】[PA2014]Pakowanie 状压dp
    【bzoj1441】Min 扩展裴蜀定理
  • 原文地址:https://www.cnblogs.com/qiangayz/p/8577376.html
Copyright © 2020-2023  润新知