0
哪裏我現在看起來是這樣的:在子類中重寫父類中的字段財產
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
字段。
你是什麼意思*做 「一個明智的方式」 *?你能否給出你試圖用這個做什麼,你期望什麼以及發生了什麼? – jonrsharpe