python物件導向程式設計三 繼承和多型

2021-10-11 04:54:30 字數 4576 閱讀 6561

在oop【物件導向程式設計——object oriented programming】程式設計中,定義乙個class的時候,可以從現有的class繼承,新的class稱為子類(subclass),而被繼承的class稱為基類、父類或超類(base class、super class)

class

animal

(object):

defrun

(self)

:print

('animal is running'

)animal = animal(

)animal.run(

)c:\users\33066\pycharmprojects\untitled\venv\scripts\python.exe c:

/users/

33066

/desktop/nine.py

animal is running

process finished with exit code 0

class

animal

(object):

defrun

(self)

:print

('animal is running'

)class

dog(animal)

:def

run(self)

:print

('dog is running'

)class

cat(animal)

:def

run(self)

:print

('dog is running'

)animal=animal(

)dog=dog(

)cat=cat(

)animal.run(

)dog.run(

)cat.run(

)c:\users\33066\pycharmprojects\untitled\venv\scripts\python.exe c:

/users/

33066

/desktop/nine.py

animal is running

dog is running

dog is running

process finished with exit code 0

就是子類獲得了父類的全部功能run()

class

animal

(object):

defrun

(self)

:print

('animal is running'

)class

dog(animal)

:pass

class

cat(animal)

:pass

dog=dog(

)dog.run(

)cat=cat(

)cat.run(

)c:\users\33066\pycharmprojects\untitled\venv\scripts\python.exe c:

/users/

33066

/desktop/nine.py

animal is running

animal is running

process finished with exit code 0

類似於python的其他資料型別,list、dict、str,class所編寫的類也是一種資料型別

用isinstance()可以判斷變數是什麼資料類# 多型

類似於python的其他資料型別,list、dict、str,class所編寫的類也是一種資料型別

用isinstance()可以判斷變數是什麼資料型別

子類增加方法

當子類的方法與父類的方法同名時,直接呼叫子類的方法

class

animal

(object):

defrun

(self)

:print

('animal is running'

)class

dog(animal)

:def

eat(self)

:print

('dog is eat'

)def

run(self)

:print

('dog is running'

)class

cat(animal)

:def

eat(self)

:print

('cat is eat'

)def

run(self)

:print

('cat is running'

)dog=dog(

)dog.eat(

)dog.run(

)cat=cat(

)cat.eat(

)cat.run(

)c:\users\33066\pycharmprojects\untitled\venv\scripts\python.exe c:

/users/

33066

/desktop/nine.py

dog is eat

dog is running

cat is eat

cat is running

process finished with exit code 0

class animal(object):

def run(self):

print('animal is running')

class dog(animal):

def eat(self):

print('dog is eat')

def run(self):

print('dog is running')

class cat(animal):

def eat(self):

print('cat is eat')

def run(self):

print('cat is running')

a = list() # a是list型別

b = animal() # b是animal型別

c = dog() # c是dog型別

d = cat()#d市cat型別

print(isinstance(a, list))

print(isinstance(b, animal))

print(isinstance(c, dog))

print(isinstance(d, cat))

c:\users\33066\pycharmprojects\untitled\venv\scripts\python.exe c:/users/33066/desktop/nine.py

true

true

true

true

process finished with exit code 0

class

animal

(object):

defrun

(self)

:print

('animal is running'

)class

dog(animal)

:def

run(self)

:print

('dog is running'

)class

cat(animal)

:def

run(self)

:print

('cat is running'

)def

run_

(animal)

: animal.run(

) animal.run(

)run_(animal())

run_(dog())

run_(cat())

class

tortoise

(animal)

:def

run(self)

:print

('tortoise is running'

)run_(tortoise())

c:\users\33066\pycharmprojects\untitled\venv\scripts\python.exe c:

/users/

33066

/desktop/nine.py

animal is running

animal is running

dog is running

dog is running

cat is running

cat is running

tortoise is running

tortoise is running

process finished with exit code 0

python物件導向程式設計 繼承

物件導向三大特性 封裝根據職責將屬性和方法封裝到乙個抽象的類中 繼承實現 的重用,相同的 不需要重複的編寫 多型不同的物件呼叫相同的方法,產生不同的執行結果,增加 的靈活度 1 繼承的語法class 類名 父類名 pass2 專業術語 3 繼承的傳遞性 子類擁有父類以及父類的父類中封裝的所有屬性和方...

JavaScript物件導向程式設計 繼承(三)

前面學習了類式繼承和建構函式繼承組合使用,也就是組合繼承,但是這種繼承方式有個問題,就是子類不是父類的例項,而子類的原型是父類的例項。子類不是父類例項的問題是由類式繼承引起的。因此還有一種更好的繼承方式,那就是寄生組合式繼承,也就是寄生式繼承和建構函式繼承的組合,因為寄生式繼承依託於原型繼承,原型繼...

物件導向程式設計 繼承

繼承是物件導向程式設計的主要特點之一。繼承,顧名思義就是子繼承父的所有。在面向程式設計中繼承的意思並沒變,子類繼承父類所擁有的屬性 方法。使用extends關鍵字使子類繼承父類,子類就可以自動復用父類的方法了 私有方法除外 並且繼承了父類的所有屬性。在子類例項化過程中子類的構造方法一定會去呼叫父類的...