【问题标题】:Create a list of a class and access it from another class创建一个类的列表并从另一个类访问它
【发布时间】:2016-06-05 15:45:03
【问题描述】:

我需要解决的是:

Create class "Scores" and "Player". 
Class score should have attributes level, score, time. 
Class player should have as information a name and a list of Scores, 
In class player implement the method maxLevel(), 
    which returns the max level achieved by the player.

我觉得这有点容易,但我还是想不通。

  1. 如何才能准确地列出分数?
  2. 完成上述操作后,如何访问每个玩家的得分列表?

谢谢!

我试过了,但我只知道我做错了。

class Score():
    level = 0
    score = 0
    time = 0

    def __init__(self,level,score,time):
        Score.level = level
        score.score = score
        Score.time = time

class Player():
    def __init__(self, name, scores):
        self.name = name
        self.scores = scores
    def maxLevel():
        ##Do stuff to calculate the max


John = Player("John", [Score(100,1456,50), Score(210,1490,100)])


John.maxLevel()

【问题讨论】:

  • 请展示您的研究和尝试。
  • 已编辑,一定很糟糕,我是真正的初学者,谢谢!

标签: python list class attributes


【解决方案1】:

我建议创建代表一个对象而不是它们的列表的类。我会将“Scores”类的名称更改为“Score”,然后我会根据需要制作尽可能多的分数对象。然后我可以创建一个名为分数的列表。现在将每个分数对象附加到分数列表中。现在要访问每个玩家类型 Player.scores 的分数,这将显示整个列表,Players.scores[1] 将选择列表中的第二个元素。

class Score(object):
    level = 0
    score = 0
    time = 0

    def __init__(self,level,score,time):
        self.level = level
        self.score = score
        self.time = time

class Player(object):
    def __init__(self, name, scores):
        self.name = name
        self.scores = scores

    def maxLevel(self):
        max_level = 0
        for score in self.scores:
            if score.level > max_level:
                max_level = score.level

        return max_level

if __name__ == "__main__":
    jon = Player("John", [Score(100,1456,50), Score(210,1490,100)])
    # Player Name
    print "Name:", jon.name
    # Score Objects
    print "Score Objects:", jon.scores
    # Score Level of 1st score
    print "Score Level 1st Score:", jon.scores[0].level
    # Score Level of 2nd score
    print "Score level 2nd Score:", jon.scores[1].level

    # Max Level info
    max_level = jon.maxLevel()
    print "Max-Level:", max_level

【讨论】:

  • 嗯,谢谢你的回复。问你是否可以用一些代码来解释这一点是否太过分了?谢谢
【解决方案2】:

我认为主要问题是您已将level, score, time 定义为静态类变量。

class Score():
    #level = 0  "these should not be here"
    #score = 0
    #time = 0

    def __init__(self,level,score,time):
        #you are not defining them for a class, but for a class instance, so each individual instance of the object score
        self.level = level  
        self.score = score
        self.time = time

class Player():
    def __init__(self, name, scores):
        self.name = name
        self.scores = scores
    def maxLevel():
        ##Do stuff to calculate the max


John = Player("John", [Score(100,1456,50), Score(210,1490,100)])


John.maxLevel()

另外,如果分数类没有任何其他属性或特殊方法,请考虑使用namedtuple 类。它更好地服务于一个简单的目的。

from collections import namedtuple

Score = namedtuple('Score', ['level', 'score', 'time'])    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多