您好,登錄后才能下訂單哦!
```#
# 經(jīng)典類VS新式類
class person(object): #這就是新式類
pass
#super(Teacher,self).__init__(name,age,sex) # 新式類寫法
class Person: #這就是經(jīng)典類
pass
#School.__init__(self,name,age,sex) #經(jīng)典類寫法
## 新式類(先從左往右找,B沒(méi)有就找C,C沒(méi)有就找A!這種查詢叫廣度查詢,先查完B,C這一層,再去查A層)
class A(object):
def __init__(self):
self.n="A"
class B(A):
pass
#def __init__(self):
#self.n="B"
class C(A):
def __init__(self):
self.n="C"
class D(B,C):
pass
# def __init__(self):
# self.n="D"
d=D()
print(d.n)
## 經(jīng)典類(經(jīng)典類在python2中先找B,B沒(méi)有就去找A!這叫深度查詢,如果A里沒(méi)找到就去找C)(新式類和經(jīng)典類在pythin3中都是廣度查詢!)
class A:
def __init__(self):
self.n="A"
class B(A):
#pass
#def __init__(self):
#self.n="B"
class C(A):
#pass
def __init__(self):
self.n="C"
class D(B,C):
pass
#def __init__(self):
#self.n="D"
d=D()
print(d.n)
# 多態(tài)
class Animal(object):
def __init__(self, name): # Constructor of the class
self.name = name
def talk(self): # Abstract method, defined by convention only
raise NotImplementedError("Subclass must implement abstract method")
class Cat(Animal):
def talk(self): #重新定義talk
print('%s: 喵喵喵!' % self.name)
class Dog(Animal):
def talk(self): #重新定義talk
print('%s: 汪!汪!汪!' % self.name)
def func(obj): # 一個(gè)接口,多種形態(tài)(傳入不同的實(shí)例,調(diào)用的接口都是talk,單表達(dá)方式不同)
obj.talk()
c1 = Cat('小晴')
d1 = Dog('李磊')
func(c1)
func(d1)
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。