因此,我只是學習如何在一個調用中集成兩個類,並且我一直在print語句中得到錯誤:TypeError:'str'對象不可調用。我是否需要創建一個str構造函數?或者我需要將兩個班級整合爲一個班級?TypeError:'str'object is not callable ... in OOP
class Election:
def __init__(self, place, ballot):
self.place=place
self.ballot=ballot
def place(self):
return self.place
def getBallot(self):
return self.ballot
def getContest(self,number):
if self.ballot[number]:
return self.ballot[number]
else:
return False
def size(self):
return len(self.ballot)
def addContest(self,Contest):
self.ballot=self.ballot.append(Contest)
return self.ballot.get(Contest)
class Contest:
def __init__(self, position, cands):
self.position = position
self.cands = {"Jones":0,"Smith":0}
def getPosition(self):
return str(self.position)
def getCandidates(self):
return str(self.cands)
def getTally(self,candidate):
return self.cands[candidate]
def addTally(self,candidate,n_votes):
self.cands[candidate]=self.cands[candidate]+n_votes
def main():
elect = Election('TestTown', [
Contest('Senator', ['Jones', 'Smith']),
Contest('Mayor', ['Anderson', 'Green'])])
print(elect.place())
elect.getContest(1)# --> <Election object at 0x7f4b359cf5d0>
elect.getContest(1).getTally('Anderson')# --> 0
main()