繼承 繼承:就像遺傳一樣,繼承就是擁有父類的所有方法和屬性,並且能夠定義自己獨特的屬性和方法,對上面的類進行擴展。 可以什麼都不寫,直接繼承父類,如下: 上面代碼可以看出,首先定義了一個People的類,還有一個Man(People)類直接繼承People類。 下麵,我們在上面的Man()類中定義一 ...
繼承
繼承:就像遺傳一樣,繼承就是擁有父類的所有方法和屬性,並且能夠定義自己獨特的屬性和方法,對上面的類進行擴展。
可以什麼都不寫,直接繼承父類,如下:
class People(object): def __init__(self,name,age): self.name = name self.age = age def eat(self): print("%s is eating......" %self.name) def sleep(self): print("%s is sleeping....." %self.name) def talk(self): print("%s is talking......" %self.name) class Man(People): pass class Woman(People): pass m1 = Man("chenronghua",16) m1.eat()
運行結果如下:
chenronghua is eating......
上面代碼可以看出,首先定義了一個People的類,還有一個Man(People)類直接繼承People類。
下麵,我們在上面的Man()類中定義一個單獨的方法,如下:
class People(object): def __init__(self,name,age): self.name = name self.age = age def eat(self): print("%s is eating......" %self.name) def sleep(self): print("%s is sleeping....." %self.name) def talk(self): print("%s is talking......" %self.name) class Man(People): def piao(self): print("%s is piaoing......20s......" %self.name) class Woman(People): pass m1 = Man("chenronghua",16) m1.piao()
運行結果如下:
chenronghua is piaoing......20s......
上面,我們給Man()增加了新的方法,可以看出,能夠執行。
下麵,我們來重寫父類。擴展新功能,子類並且要具有父類的所有方法與屬性。在子類中調用父類:
class People(object): def __init__(self,name,age): self.name = name self.age = age def eat(self): print("%s is eating......" %self.name) def sleep(self): print("%s is sleeping....." %self.name) def talk(self): print("%s is talking......" %self.name) class Man(People): def piao(self): print("%s is piaoing......20s......" %self.name) def sleep(self): People.sleep(self) print("man is sleeping......") class Woman(People): pass m1 = Man("chenronghua",16) m1.sleep()
上面代碼中,子類調用了父類中的方法,要明白本質,創建實例的本質是增加了一個新的記憶體變數。
不同類中方法的調用:
class People(object): def __init__(self,name,age): self.name = name self.age = age def eat(self): print("%s is eating......" %self.name) def sleep(self): print("%s is sleeping....." %self.name) def talk(self): print("%s is talking......" %self.name) class Man(People): def piao(self): print("%s is piaoing......20s......" %self.name) def sleep(self): People.sleep(self) print("man is sleeping......") class Woman(People): def get_birth(self): print("%s is borning a bady......" %self.name) # m1 = Man("chenronghua",16) # m1.sleep() w1 = Woman("chenronghua",26) w1.get_birth() w1.piao()
運行如下:
chenronghua is borning a bady......
Traceback (most recent call last):
File "/home/zhuzhu/第六天/inherit.py", line 34, in <module>
w1.piao()
AttributeError: 'Woman' object has no attribute 'piao'
從上面可以看出,繼承同一個父類的子類是方法是不能相互調用的。
class People(object): def __init__(self,name,age): self.name = name self.age = age def eat(self): print("%s is eating......" %self.name) def sleep(self): print("%s is sleeping....." %self.name) def talk(self): print("%s is talking......" %self.name) class Man(People): def __init__(self,name,age,money): #給男的單獨增加屬性 People.__init__(self,name,age) self.money = money print("%s 剛出生就有%s元" %(self.name,self.money)) #構造函數的時候就會執行代碼 def piao(self): print("%s is piaoing......20s......" %self.name) def sleep(self): People.sleep(self) print("man is sleeping......") class Woman(People): def get_birth(self): print("%s is borning a bady......" %self.name) m1 = Man("chenronghua",16,100) m1.sleep()
運行結果如下:
chenronghua 剛出生就有100元
chenronghua is sleeping.....
man is sleeping.....
從上面代碼可以看出,__init__(self,name,age,money)類的初始化,People.__init__(self,name,age)繼承,繼承父類的屬性,普通的繼承。
下麵來看看新式類中的繼承。
class People(object): def __init__(self,name,age): self.name = name self.age = age def eat(self): print("%s is eating......" %self.name) def sleep(self): print("%s is sleeping....." %self.name) def talk(self): print("%s is talking......" %self.name) class Man(People): def __init__(self,name,age,money): #給男的單獨增加屬性 super(Man,self).__init__(name,age) self.money = money print("%s 剛出生就有%s元" %(self.name,self.money)) #構造函數的時候就會執行代碼 def piao(self): print("%s is piaoing......20s......" %self.name) def sleep(self): People.sleep(self) print("man is sleeping......") class Woman(People): def get_birth(self): print("%s is borning a bady......" %self.name) m1 = Man("chenronghua",16,100) m1.sleep()
新式類是用super()函數來實現繼承的,super(Man,self).__init__(name,age)實現繼承,新式類和舊式類的差別主要體現在多繼承上面。
下麵看下類的多繼承問題:
class People(object): def __init__(self,name,age): self.name = name self.age = age def eat(self): print("%s is eating......" %self.name) def sleep(self): print("%s is sleeping....." %self.name) def talk(self): print("%s is talking......" %self.name) class Relation(object): def make_friends(self,obj): print("%s is making friends with %s" %(self.name,obj.name)) class Man(People,Relation): def __init__(self,name,age,money): #給男的單獨增加屬性 super(Man,self).__init__(name,age) #超級父類,Man繼承父類name,age self.money = money print("%s 剛出生就有%s元" %(self.name,self.money)) #構造函數的時候就會執行代碼 def piao(self): print("%s is piaoing......20s......" %self.name) def sleep(self): People.sleep(self) print("man is sleeping......") class Woman(People,Relation): def get_birth(self): print("%s is borning a bady......" %self.name) w1 = Woman("ruhua",18) m1 = Man("chenronghua",16,100) m1.make_friends(w1) #把w1實例當做參數傳給make_friends,等價於obj》w1,obj.name》w1.name
上面代碼中,當子類繼承多個父類的時候,即便其中一個父類沒有實例化,也能調用另外一個父類的方法和屬性。通過子類把兩個父類關聯到一起。
多態
一個介面,多個重用