[英]What's best way to pass different insances of object to multiple classes?
States
class is a superclass for Game
and MainMenu
because they gonna use attributes with the same names. States
类是Game
和MainMenu
的超类,因为它们将使用具有相同名称的属性。 In the project I'm gonna use only one instance of Game
and MainMenu
class.在项目中,我将只使用Game
和MainMenu
类的一个实例。 GameData
is a class which loads(from files) and store data like images, sounds etc. and it's attributes are read-only. GameData
是一个加载(从文件)和存储图像、声音等数据的类,它的属性是只读的。 I'd like to pass GameData
object to the Game
and MainMenu
instances and I wonder, if my solution where I put it in the superclass is correct or there's maybe better way?我想将GameData
对象传递给Game
和MainMenu
实例,我想知道我将它放在超类中的解决方案是否正确或者有更好的方法?
from game_data import GameData
class States:
"""Superclass for each state"""
game_data = GameData() # Is it correct and 'pythonic'?
def __init__(self):
self.done = False
self.next = None
self.quit = False
self.previous = None
class Game(States):
def __init__(self):
States.__init__(self)
def some_method(self):
pass # Here I'm gonna use game_data
class MainMenu(States):
def __init__(self):
States.__init__(self)
def some_method(self):
pass # Here I'm gonna use game_data
Other solution but I think it's incorrect because it's not optimal(2 instances of GameData()):其他解决方案,但我认为这是不正确的,因为它不是最佳的(GameData() 的 2 个实例):
from game_data import GameData
class States:
"""Superclass for each state"""
def __init__(self):
self.done = False
self.next = None
self.quit = False
self.previous = None
class Game(States):
def __init__(self):
States.__init__(self)
self.game_data = GameData() # First instance
def some_method(self):
pass # Here I'm gonna use self.game_data
class MainMenu(States):
def __init__(self):
States.__init__(self)
self.game_data = GameData() # Second instance
def some_method(self):
pass # Here I'm gonna use self.game_data
Or maybe it should be passed as an argument?:或者它应该作为参数传递?:
from game_data import GameData
class States:
"""Superclass for each state"""
def __init__(self):
self.done = False
self.next = None
self.quit = False
self.previous = None
class Game(States):
def __init__(self, game_data): # As arg
States.__init__(self)
self.game_data = game_data # Assignment
def some_method(self):
pass # Here I'm gonna use self.game_data
class MainMenu(States):
def __init__(self, game_data): # As arg
States.__init__(self)
self.game_data = game_data # Assignment
def some_method(self):
pass # Here I'm gonna use self.game_data
game_data = GameData()
game = Game(game_data)
main_menu = MainMenu(game_data)
Is one of these solutions correct or it should be done in another way?这些解决方案中的一个是正确的还是应该以另一种方式完成?
It look to me that the States
class seems to be for handling the game states and the GameData
class as you said is a read-only class for images, sounds, etc.在我看来, States
类似乎用于处理游戏状态,而GameData
类如您所说是用于图像、声音等的只读类。
So instead of initializing its object in the classes, you can just make the whole GameData
class as a static class ie make every methods in the class as @staticmethod
.因此,不是在类中初始化它的对象,您可以将整个GameData
类设为静态类, GameData
类中的每个方法设为@staticmethod
。
Then you can just import the GameData
class and use its methods in the MainMenu
and Game
classes whenever needed using the syntax GameData.some_static_method()
.然后,您可以在需要时使用语法GameData.some_static_method()
导入GameData
类并在MainMenu
和Game
类中使用其方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.