在Python 3.6,比方说,我有一个抽象类MyAbstractClass
如何写一个抽象的财产在Python 3.4+
from abc import ABC, abstractmethod
class MyAbstractClass(ABC):
@property
@abstractmethod
def myProperty(self):
pass
,并从它一类MyInstantiatableClass
继承。那么如何在属性myProperty
上写实例化这个类的对象?我希望能够设置并获得myProperty
。下面不起作用。
from MyAbstractClass import MyAbstractClass
class MyInstantiatableClass(MyAbstractClass):
def __init__(self, desiredValueOfMyProperty):
????
@myProperty.setter
def myProperty(self, desiredValueOfMyProperty): # value coming from __init__
self._myProperty = desiredValueOfMyProperty
和一个主功能,比方说,
from MyInstantiatableClass import MyInstantiatableClass
def main():
MyInstantiatableClass(3) # 3 is the desiredValueOfMyProperty for this instantiation
MyInstantiatableClass(5) # 5 is the desiredValueOfMyProperty for this instantiation
你是否正在实施_only_ setter,没有getter? –
我希望能够在'MyInstantiatableClass'上设置和获取'myProperty'。 – Bora
按照通常的方式定义属性。 – user2357112