Python 多型與動態繫結

2021-07-22 09:52:30 字數 3514 閱讀 7165

# coding: utf-8

class

geometricobject:

def__init__

(self, color = "green", filled = "true"):

self.__color = color

self.__filled = filled

defgetcolor

(self):

return self.__color

defsetcolor

(self, color):

self.__color = color

defisfilled

(self):

return self.__filled

defsetfilled

(self, filled):

self.__filled = filled

def__str__

(self):

return

"color: " + self.__color + \

" and filled: " + str(self.__filled)

# coding: utf-8

from geometricobject import geometricobject

import math

class

circle

(geometricobject):

def__init__

(self, radius):

geometricobject.__init__(self)

self.__radius = radius

defgetradius

(self):

return self.__radius

defsetradius

(self, radius):

self.__radius = radius

defgetarea

(self):

return self.__radius * self.__radius * math.pi

defgetdiameter

(self):

return

2 * self.__radius

defgetperimeter

(self):

return

2 * self.__radius * math.pi

defprintcircle

(self):

print self.__str__() + "radius: " + str(self.__radius)

# override the __str__() method defined in geometricobject

def__str__

(self):

return geometricobject.__str__(self) + " radius: " + str(self.__radius)

from geometricobject import geometricobject

class

rectangle

(geometricobject):

def__init__

(self, width = 1, height = 2):

geometricobject.__init__(self)

self.__width = width

self.__height = height

defgetwidth

(self):

return self.__width

defsetwidth

(self, width):

self.__width = width

defgetheight

(self, height):

return self.__height

defsetheight

(self, height):

self.__height = height

defgetarea

(self):

return self.__width * self.__height

defgetperimeter

(self):

return

2 * (self.__width + self.__height)

# override the __str__ method defined in geometricobject

def__str__

(self):

return geometricobject.__str__(self) + " width: " + \

str(self.__width) + " height: " + str(self.__height)

# coding: utf-8

from circlefromgeometricobject import circle

from rectanglefromgeometricobject import rectangle

defmain

():# display circle and rectangle properties

c = circle(4)

r = rectangle(1, 3)

displayobject(c)

displayobject(r)

print

"are the circle and rectangle the same size? " , \

issamearea(c, r)

# display geometric object properties

defdisplayobject

(g):

print g.__str__()

# compare the areas of two geometric objects

defissamearea

(g1, g2):

return g1.getarea() == g2.getarea()

main()

在這個例子中,c是circle類的乙個物件。circle類是geometricobject類的子類。str()方法在這兩個類中都有定義。因此,在displayobject方法中g應當呼叫哪個str()方法?g應當呼叫哪個str()方法由 動態繫結 決定。

動態繫結的機制如下: 假設物件o是類c1,c2,c3,…,cn(c1是c2的子類,c2是c3的子類,… ,cn-1 是 cn 的子類,即cn是通用的類,c1是最特定的類)的例項。在python中,cn是object類。如果物件o呼叫乙個方法p,那麼python會依次在類c1,c2,…,cn中查詢方法p的實現,直到找到為止。一旦找到乙個實現,就會停止查詢然後呼叫這個第一次找到的實現。

多型,動態繫結

呼叫方法時,只要方法重寫了,實際當中調哪個,要看實際中new哪個物件。color red 好處 可擴充套件性達到最好。color 當增加東西時,不需要改原來的結構,直接加上就可以了。color red 多型的存在必要條件 1 要有繼承,2 要有重寫,3 父類引用指向子類物件。color packag...

動態繫結和多型

動態繫結是指 在執行期間 即非編譯期 判斷所引用物件的實際型別,根據其實際的型別呼叫其相應的方法。多型的存在有三個必要條件 1,要求繼承 2,要有重寫 3,父類引用指向子類物件 例 class animal public void enjoy class cat extends animal pub...

多型 動態繫結 實驗

定義三個類,父類geometricobject代表幾何形狀,子類circle代表圓形,myrectangle代表矩形。定義乙個測試類test,編寫equalsarea方法測試兩個物件的面積是否相等 注意方法的引數型別,利用動態繫結技術 編寫displaygeometricobject方法顯示物件的面...