[英]Override property setter abstract method in child class
I've been exploring the @property
decorator and abstract classes for the first time and followed along with the Python docs to define the following classes: 我第一次探索@property
装饰器和抽象类,然后跟着Python文档来定义以下类:
In [125]: from abc import ABC, abstractmethod
...: class C(ABC):
...: def __init__(self):
...: self._x = None
...: @property
...: def x(self):
...: """I'm the 'x' property"""
...: return self._x
...: @x.setter
...: @abstractmethod
...: def x(self, value):
...: self._x = value
...: @x.deleter
...: def x(self):
...: del self._x
...: class D(C):
...: pass
The setter property in class C is an abstractmethod
. C类中的setter属性是一个abstractmethod
。 I'm aware that I must override this setter property(method) in class D in order to instantiate an object, but I'm unsure how to do that as all methods in class C are named the same. 我知道我必须在D类中重写这个setter属性(方法)才能实例化一个对象,但我不确定如何做到这一点,因为C类中的所有方法都被命名为相同。 Now, I know, I could just call the methods getx()
, setx()
etc. in class C and just override setx()
in class D, but I want to know if there's a way to do it with the property syntax above. 现在,我知道,我可以在类C中调用方法getx()
, setx()
等,并在类D中重写setx()
,但我想知道是否有办法使用上面的属性语法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.