6
我寫測試代碼3類,並使用責任設計鏈模式,下面的Python - 困惑繼承
我打印print(c._abc is b._abc)
代碼,答案是真的,但我原來認爲,這兩個是不同的。
然後,第2輪,我取消註釋self._abc = kwargs
並評論其他3行, 答案變成False。
這是爲什麼?
import abc
class A:
__metaclass__ = abc.ABCMeta
_abc = {}
def __init__(self,successor=None,**kwargs):
self._successor = successor
@abc.abstractmethod
def handlerRequest(self):
pass
class B(A):
def __init__(self,successor=None,**kwargs):
self._successor = successor
print(kwargs)
# self._abc = kwargs # round 2<---uncomment here
self._abc['a'] = kwargs['a'] # round 2<---comment here
self._abc['b'] = kwargs['b'] # round 2<---comment here
self._abc['Hello'] = 'World' # round 2<---comment here
def handlerRequest(self):
if (self._successor is not None):
self._successor.handlerRequest()
print(self._abc)
class C(A):
def handlerRequest(self):
if (self._successor is not None):
self._successor.handlerRequest()
print(self._abc)
list = {'a':1,'b':2}
b = B(**list)
c = C(b)
print(c._abc is b._abc)
c.handlerRequest()