我现在的情况是这样的:
class A(object):
def __init__(self, val):
self.x=val
self.y=42
# other fields
class B(object):
def __init__(self):
self.a=22
# other fields
class C(A,B):
def __init__(self, val):
super(C,self).__init__(val)
@property
def x(self):
# if A.x is None return a value that I can compute from A.y and B.a
# if A.x is not None return it
@x.setter
def x(self, val):
# set the field value
有时我只想手动设置
x
的假设值,这种情况下我会使用A
。在其他情况下,我想使用更复杂的方法,涉及计算基于组织为B
的信息的A.x
值。这段代码的思想是创建一个C
类,它看起来像一个A
(关于x
字段),但不需要手动设置该字段值,而是直接从派生。 我无法弄清楚如何以明智的方式使C.x
属性遮盖A.x
字段。