我有一個代碼如何從python的子類訪問超類的成員和變量?
class Trigger(object):
def evaluate(self,story):
Trigger.stroy=story
WordTrigger.isWordin(story)
raise NotImplementedError
class WordTrigger(Trigger):
def __init__(self,word):
self.word=word
def isWordin(self, text):
rep = text.replace(string.punctuation, '')
toLower = rep.lower()
text = toLower.split(' ')
if(text in self.word):
return True
else:
return False
class TitleTrigger(WordTrigger):
...
變量story
是一類
class NewsStory: def __init__(self,guid, title, subject, summary, link): self.guid=guid self.title=title self.subject=subject self.summary=summary self.link=link def getGuid(self): return self.guid def getTitle(self): return self.title def getSubject(self): return self.subject def getSummary(self): return self.summary def getLink(self): return self.link
story=NewsStory(...)
如何訪問變量story
其內置的高清功能的子類TitleTrigger
?
最簡單的方法是用'Trigger.story'。如果你只是閱讀它(不分配新值),'self.story'也可以工作。如果您有任何不能使用的理由,請進一步解釋。 – BrenBarn