python中的interface, abstract class, class property
1. python class的繼承
python允許多根繼承, 這點(diǎn)像C++, 但不像C++那樣變態(tài), 需區(qū)分公有繼承/私有繼承/保護(hù)繼承, python只有一種繼承方式。也許正因?yàn)橹С侄嘀乩^承, 因此python沒有interface這個(gè)關(guān)鍵詞.
2. 給類起個(gè)別名
在python中, class也是對象, 所以你可以像操作對象一樣, 將class賦值給一個(gè)對象, 這樣就相當(dāng)于給class起了一個(gè)別名
可以在代碼中:
ShortName = MyReallyBigClassNameWhichIHateToType
或者在import時(shí)候,
from modulename import ReallyLongNameWhichIHateToType as FriendlyName
3. 如何訪問父類的成員
大概有3中方法吧:
a. 可以在子類中, 直接通過父類名來調(diào)用父類的成員,
b. 也可以先給父類起個(gè)別名, 然后使用別名來訪問父類成員.
c. 使用super()內(nèi)建方法, 但使用super(), 有時(shí)候并不像你想象的那樣, 下面的鏈接有更多的信息
python中的super
http://only-u.appspot.com/2010/04/29/python.html
http://kanrss.com/@kevin/t/1447052
MRO & super
http://blog.csdn.net/seizeF/archive/2010/02/18/5310107.aspx
代碼
#-----------------------
from abc import ABCMeta, abstractmethod
class Drawable():
__metaclass__ = ABCMeta
@abstractmethod
def draw(self, x, y, scale=1.0):
pass
def draw_doubled(self, x, y):
self.draw(x, y, scale=2.0)
class Square(Drawable):
def draw(self, x, y, scale): #####
pass
c=Square()
#-----------------------
代碼
#---------使用property()的例子-------------------------
class C(object):
y = 3
z = 4
def __init__(self):
self.__x = 2
def getx(self):
return self.__x
def setx(self, val):
print "x is read only"
x = property(getx, setx) #這不是真正的只讀屬性, 雖然在setx中,沒有更新__x, 但你仍可對x屬性賦值, 雖然復(fù)制不生效, 但也不報(bào)錯(cuò)
x = property(getx) #這是真正的只讀屬性, 不給屬性增加setter, 這是真正的只讀屬性, 因?yàn)橐坏┮ox賦值的時(shí)候, 運(yùn)行就報(bào)錯(cuò)
x = property(lambda self: self.__x)#這是真正的只讀屬性, 因?yàn)橐坏┮ox賦值的時(shí)候, 運(yùn)行就報(bào)錯(cuò)
c=C()
print(c.x,c.y,c.z)
c.x=3
print(c.x,c.y,c.z)
#----------------------------------
代碼
#----------使用@property的例子------------------------
class B(object):
def __init__(self):
self._x = None
@property
def x(self): #這是x的getter
"""I'm the 'x' property."""
return self._x
@x.setter
def x(self, value): #這是x的setter
self._x = value
@x.deleter
def x(self): #這是x的deleter
del self._x
b=B()
b.x=12
print(b.x)
#----------------------------------
代碼
#--------使用abstractproperty()------------------------------
from abc import ABCMeta
from abc import abstractproperty
from abc import abstractmethod
class IMediaPlayer_1:
"""API for accessing a media player"""
__metaclass__ = ABCMeta
def get_volume(self):
pass
def set_volume(self, value):
pass
volume = abstractproperty(get_volume, set_volume,
doc="Return or set volume: 0..100")
class WinMediaPlay_1(IMediaPlayer_1):
def __init__(self):
self._volume=0
def get_volume(self):
return self._volume
def set_volume(self, value):
self._volume=value
volume = property(get_volume, set_volume,
doc="Return or set volume: 0..100")
player1=WinMediaPlay_1()
player1.volume=10
print(player1.volume)
#--------------------------------------
代碼
#-----------使用@abstractproperty---------------------------
from abc import ABCMeta
from abc import abstractproperty
from abc import abstractmethod
class IMediaPlayer_2:
"""API for accessing a media player"""
__metaclass__ = ABCMeta
@abstractproperty
def price(self):
"""I'm the 'x' property."""
pass
@price.setter
def price(self, value):
pass
class WinMediaPlay_2(IMediaPlayer_2):
def __init__(self):
self._price=0
@property
def price(self):
"""I'm the 'x' property."""
return self._price
@price.setter
def price(self, value):
self._price=value
player2=WinMediaPlay_2()
player2.price=20
print(player2.price)
#--------------------------------------
7. python 類的 static variable
在類的__init__()中, 引出的變量為實(shí)例變量, 直接在類塊中引出的變量為靜態(tài)變量.
8. python 類的 static method 和 class method
python中有static method 和 class method之分, 一般講, 差異不大, 可以混著用.
@staticmethod decorator之后的方法為static方法.
@classmethod decorator之后的方法為類方法, 它的第一個(gè)參數(shù)必須為cls, (注:實(shí)例方法的第一個(gè)參數(shù)是self). 如果你是通過sub_class來調(diào)用base_class的一個(gè)classmethod, 那么函數(shù)體中, cls.__name__為sub_class, 而不是base_class. 正是因?yàn)檫@點(diǎn), python的類方法比static方法更流行一些, 這尤其是在工廠類中特別有用.


浙公網(wǎng)安備 33010602011771號