简体   繁体   English

将不同的对象实例传递给多个类的最佳方法是什么?

[英]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类是GameMainMenu的超类,因为它们将使用具有相同名称的属性。 In the project I'm gonna use only one instance of Game and MainMenu class.在项目中,我将只使用GameMainMenu类的一个实例。 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对象传递给GameMainMenu实例,我想知道我将它放在超类中的解决方案是否正确或者有更好的方法?

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类并在MainMenuGame类中使用其方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将对象传递给多个继承的类的更好方法 - Better way to pass an object to multiple inherited classes 在 Python 个类中初始化和使用常量的最佳方法是什么? - What's the best way to initialise and use constants across Python classes? 基于原始列的 dtype 作为对象在数据框中创建多个虚拟变量的最佳方法是什么? - What is the best way to create multiple dummy variables in a data frame based on the original column's dtype being an object? 链接视图和在视图之间传递数据的最佳方法是什么? - What's the best way to link views and pass data between them? 在python中将多个参数传递给init类的最佳方法是什么? - What is the best way to pass multiple parameters to class init in python? 初始化对象时传递方法的最佳方法是什么 - What is the best way to pass a method when I initialize an object 在python中将对象转换为列表的最佳方法是什么? - What's the best way convert object to list in python? 定制Zope对象的获取链的最佳方法是什么? - What is the best way to customize a Zope object's acquisition chain? 获取引用对象(可变级别)的最佳方法是什么 - What's the best way to get referenced object (variable level) 从多个目录加载大量图像的最佳方法是什么? - What's the best way to load a lot of images from multiple directories?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM