这是我的班级:
class Player(object):
def __init__(self, playernum):
self.playernum = playernum
def play_turn(self, board):
"""This method is passed an instance of ConnectFour.
It should examine the board (using methods on the ConnectFour class...
assume you have it) and eventually call board.play_turn and return"""
pass
到目前为止,我了解如果我这样做:
class Human(Player):
它将使Human()成为Player的派生类。
我想要做的是在这个类中有一个构造函数playernum。然后采取被覆盖的play_turn并打印一个玩家编号(即播放器编号)...我只是想知道如何实现这个...我会重复
def play_turn(self,board):
在Human类中,或者我只是把
class Human(Player):
play_turn
并在
内play_turn(self,board):
"put"
print playernum
我对类的派生及其背后的逻辑有点新意。任何输入都将受到高度赞赏。感谢。
答案 0 :(得分:4)
你是正确的,要从父类重写方法,你'重复'派生类内的方法。您的代码最终应该看起来像:
class Human(Player):
def play_turn(self, board):
print self.playernum
如果play_turn
意图包含其派生类的共享逻辑,则首先要调用父方法:
class Human(Player):
def play_turn(self, board):
super(Human, self).play_turn(board)
print self.playernum