我必须创建两个类Player和Group。 类Player具有属性名称和级别。创建Player实例后,将在0到99的范围内创建唯一标识符。
Class Group有属性名称。创建Group实例后,将在0到20的范围内创建一个唯一的groupIdentifier。在Group类中,我需要创建一个名为addPlayer的方法来添加类Player的实例,并检查要添加的实例是否重复或者不。 addPlayer方法返回添加到其中的Player的唯一标识符。我有以下代码:
class Player(object):
identifier =0
def __init__(self,name, level=""):
self.name = name
self.level =level
self.identifier += 1
if(len(self.name) < 1):
raise ValueError("Name.__init__: is empty")
if(self.identifier>=99):
raise ValueError("Identifier.__init__: exceeds 99")
class Group(object):
groupIdentifier =0
def __init__(self,name, player=[]):
self.name = name
self.player = player
self.groupIdentifier += 1
if(len(self.name) < 1):
raise ValueError("Name.__init__: is empty")
if(self.groupIdentifier>20):
raise ValueError("GroupIdentifier.__init__: exceeds 20")
def addPlayer(self, myPlayer):
if myPlayeris None:
raise ValueError("myPlayer: is empty")
for x in range(len(self.player)):
if self.player[x]==(myPlayer):
raise ValueError("Player: is duplicate")
self.participant.append(myPlayer)
return self.player.identifier
您能否建议我在代码中进行更改以满足要求?
答案 0 :(得分:1)
使用class属性为玩家(和组!)提供标识符,而不是实例属性:
class Player(object):
identifier = 0
def __init__(self,name, level=""):
self.name = name
self.level =level
# identifier belong to the class not an instance of it!
# change in Group as well!
self.identifier == Player.identifier
Player.identifier += 1
if(len(self.name) < 1): # can be 'if not self.name'
raise ValueError("Name.__init__: is empty")
if(self.identifier>=99):
raise ValueError("Identifier.__init__: exceeds 99")
覆盖__eq__
中的Player
,以便能够按照标识符对玩家进行比较:
class Player(object):
# ... init code
def __eq__(self, other):
if isinstance(other, Player):
# other object is a Player instance with the same identifier
return self.identifier == other.identifier
return False
您可能还想在self.players
中使用Group
(复数)标记其为多个list
个实例的Player
这一事实。
现在更改Group
中的玩家比较位并使用in
关键字:
if myPlayer in self.players:
raise ValueError("Player: is duplicate")
更改后:
class Player(object):
identifier = 0
def __init__(self,name, level=""):
self.name = name
self.level =level
self.identifier += Player.identifier
Player.identifier += 1
if not self.name:
raise ValueError("Name.__init__: is empty")
if(self.identifier>=99):
raise ValueError("Identifier.__init__: exceeds 99")
def __eq__(self, other):
if isinstance(other, Player):
# other object is a Player instance with the same identifier
return self.identifier == other.identifier
return False
class Group(object):
groupIdentifier = 0
def __init__(self,name, players=[]):
self.name = name
self.players = players
self.groupIdentifier = Group.groupIdentifier
Group.groupIdentifier += 1
if not self.name:
raise ValueError("Name.__init__: is empty")
if(self.groupIdentifier>20):
raise ValueError("GroupIdentifier.__init__: exceeds 20")
def addPlayer(self, myPlayer):
if myPlayer is None:
raise ValueError("myPlayer: is empty")
if myPlayer in self.players:
raise ValueError("Player: is duplicate")
# removed participant, as it is unclear where it's coming from
# if you need it - add it to __init__
self.players.append(myPlayer)
return myPlayer.identifier