類的繼承,深度優先於廣度優先

2022-09-07 18:09:15 字數 3406 閱讀 5773

#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

class schoolmember(object):

def __init__(self,name,age,***):

self.name = name

self.age = age

self.*** = ***

def tell(self):

pass

class teacher(schoolmember):

def __init__(self,name,age,***,salary,course):

super(teacher,self).__init__(name,age,***)

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,***,schoolid,course):

super(student, self).__init__(name,age,***)

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)

Python 類的多重繼承 深度優先 廣度優先

一 多重繼承 1 經典類 vs 新式類 python 2.x中預設都是經典類,只有顯式繼承了object才是新式類 python 3.x中預設都是新式類,不必顯式的繼承object 2 多重繼承 python中乙個類可以同時繼承多個類 class 類名 父類1,父類2,類體二 深度優先 1 經典類採...

深度優先 廣度優先

父類定義 class people def init self,name,age,weight self.name name self.age age self.weight weight defspeak self print s 說 我 d 歲。self.name,self.age 單繼承示例 ...

深度優先和廣度優先

在爬蟲系統中,待抓取url佇列是很重要的一部分,待抓取url佇列中的url以什麼樣的順序排隊列也是乙個很重要的問題,因為這涉及到先抓取哪個頁面,後抓取哪個頁面。而決定這些url排列順序的方法,叫做抓取策略。下面是常用的兩種策略 深度優先 廣度優先 注 scrapy預設採用的是深度優先演算法 這裡是深...