python中的interface, abstract class, class property(python中的intersection函数)

网友投稿 442 2022-09-05


python中的interface, abstract class, class property(python中的intersection函数)

1. python class的继承

python允许多根继承, 这点像C++, 但不像C++那样变态, 需区分公有继承/私有继承/保护继承, python只有一种继承方式。也许正因为支持多重继承, 因此python没有interface这个关键词.

2. 给类起个别名

在python中, class也是对象, 所以你可以像操作对象一样, 将class赋值给一个对象, 这样就相当于给class起了一个别名

可以在代码中:

ShortName = MyReallyBigClassNameWhichIHateToType

或者在import时候,

from modulename import ReallyLongNameWhichIHateToType as FriendlyName

3.  如何访问父类的成员

大概有3中方法吧:

a. 可以在子类中, 直接通过父类名来调用父类的成员,

b. 也可以先给父类起个别名, 然后使用别名来访问父类成员.

c. 使用super()内建方法, 但使用super(), 有时候并不像你想象的那样, 下面的链接有更多的信息

python中的super

& super

4. python有interface和abstract class吗?

没有interface, 这个真没有!  那就用abstract class来模拟interface定义吧! 呵呵, python好像连abstract class也不是原生态的, 好在还有一个ABC(abstract base class), 将就用吧.

abstract base class

abc import ABCMeta, abstractmethodclass Drawable(): __metaclass__ = ABCMeta @abstractmethoddef draw(self, x, y, scale=1.0): passdef draw_doubled(self, x, y): self.draw(x, y, scale=2.0)class Square(Drawable):def draw(self, x, y, scale): ##### passc=Square() #-----------------------

Square类一定要实现draw()方法, 否则, 当实例化一个Square对象时, 将报错TypeError: Can't instantiate abstract class Square with abstract methods draw

5. 那么有没有C#的property概念呢?

可以有2种方式, 一个是使用x=property(getter,setter, deleter)的方式, 另一个是@property,@x.setter,@x.deleter

C(object): y = 3 z = 4def __init__(self): self.__x = 2def getx(self):return self.__xdef setx(self, val):print "x is read only" x = property(getx, setx) #这不是真正的只读属性, 虽然在setx中,没有更新__x, 但你仍可对x属性赋值, 虽然复制不生效, 但也不报错 x = property(getx) #这是真正的只读属性, 不给属性增加setter, 这是真正的只读属性, 因为一旦要给x赋值的时候, 运行就报错 x = property(lambda self: self.__x)#这是真正的只读属性, 因为一旦要给x赋值的时候, 运行就报错c=C()print(c.x,c.y,c.z)c.x=3print(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.setterdef x(self, value): #这是x的setter self._x = value @x.deleterdef x(self): #这是x的deleter del self._xb=B()b.x=12print(b.x)#----------------------------------

6. 抽象类中, 可以包括abstract property, 和property一样, 也可以使用两种方式来定义抽象属性, 使用abstractproperty()或@abstractproperty

#--------使用abstractproperty()------------------------------from abc import ABCMeta from abc import abstractpropertyfrom abc import abstractmethodclass IMediaPlayer_1:"""API for accessing a media player"""__metaclass__ = ABCMetadef get_volume(self): passdef 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._volumedef 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=10print(player1.volume) #--------------------------------------

#-----------使用@abstractproperty---------------------------from abc import ABCMeta from abc import abstractpropertyfrom abc import abstractmethod class IMediaPlayer_2:"""API for accessing a media player"""__metaclass__ = ABCMeta @abstractpropertydef price(self):"""I'm the 'x' property."""pass @price.setterdef price(self, value):passclass WinMediaPlay_2(IMediaPlayer_2):def __init__(self): self._price=0 @propertydef price(self):"""I'm the 'x' property."""return self._price @price.setterdef price(self, value): self._price=valueplayer2=WinMediaPlay_2()player2.price=20 print(player2.price) #--------------------------------------

7. python 类的 static variable

在类的__init__()中, 引出的变量为实例变量, 直接在类块中引出的变量为静态变量.

8. python 类的 static method 和 class method

python中有static method 和 class method之分, 一般讲, 差异不大, 可以混着用.

@staticmethod decorator之后的方法为static方法.

@classmethod decorator之后的方法为类方法, 它的第一个参数必须为cls, (注:实例方法的第一个参数是self). 如果你是通过sub_class来调用base_class的一个classmethod, 那么函数体中, cls.__name__为sub_class, 而不是base_class.  正是因为这点, python的类方法比static方法更流行一些, 这尤其是在工厂类中特别有用.


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:some python trap and hints(somebody that I used to know)
下一篇:【手写字母识别】基于ELMAN结合BP神经网络实现手写大写字母(A-O)识别含Matlab源码
相关文章

 发表评论

暂时没有评论,来抢沙发吧~