class Player:
def __init__(self, name=None):
self.name = name
self.score = 0
def add_score(self, score):
self.score = self.score + score
class HumanPlayer(Player):
def __init__(self):
Player.__init__(self, name="Human")
class ComputerPlayer(Player):
def __init__(self):
Player.__init__(self, name="Eliza")
class Score:
def __init__(self, human, computer):
self.human = human
self.computer = computer
def add_score(self, player, score):
self.player.add_score(score)
我的问题是在Score
类中的add_score
方法。关于在Python中继承的面向对象设计
在Python方面:
由于两个human
和computer
从Player
类继承的(他们有自己的方法,但在这个问题上省略),我怎么可以指他们在add_score
方法? (需要明确的是:我想说add_score
从Player
类继承的任何对象[不要紧,如果指的是“add_score
任何对象无论是从Player
类或从Player
类继承的任何对象”?])
是否要添加代表使用语言的标签? – Marged
对于1,你说的是在你的'score'类或'human'和'computer'类中引用'human'和'computer'的成员吗? –