类之间的Python传递变量 [英] Python-passing variable between classes

查看:404
本文介绍了类之间的Python传递变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为游戏创建角色生成向导.在一个类中,我计算角色的属性.在不同的类中,我要向用户显示基于字符属性的哪些专业可用.但是,我不记得如何在不同的类之间传递变量.

I'm trying to create a character generation wizard for a game. In one class I calculate the attributes of the character. In a different class, I'm displaying to the user which specialties are available based on the attributes of the character. However, I can't remember how to pass variables between different classes.

这是我所拥有的一个例子:

Here is an example of what I have:

class BasicInfoPage(wx.wizard.WizardPageSimple):            
    def __init__(self, parent, title):
         wiz.WizardPageSimple.__init__(self, parent)
         self.next = self.prev = None
         self.sizer = makePageTitle(self, title)

                    <---snip--->

         self.intelligence = self.genAttribs()

class MOS(wx.wizard.WizardPageSimple):
     def __init__(self, parent, title):
         wiz.WizardPageSimple.__init__(self, parent)
         self.next = self.prev = None
         self.sizer = makePageTitle(self, title)
      def eligibleMOS(self, event):
          if self.intelligence >= 12: 
               self.MOS_list.append("Analyst")

问题是我无法弄清楚如何使用从BasicInfoPage类到MOS类的"intelligence"变量.我从互联网上尝试了几种不同的方法,但是似乎没有任何效果.我想念什么?

The problem is that I can't figure out how to use the "intelligence" variable from the BasicInfoPage class to the MOS class. I've tried several different things from around the Internet but nothing seems to work. What am I missing?

编辑:发布此信息后,我意识到自己的解释不够好.我正在尝试创建1980年代的Twilight 2000 RPG的计算机版本.

Edit I realized after I posted this that I didn't explain it that well. I'm trying to create a computer version of the Twilight 2000 RPG from the 1980s.

我正在使用wxPython创建向导;我类的父类是wxPython中的向导.该向导将引导用户完成角色的创建,因此基本信息"页面(类BasicInfoPage)允许用户提供角色的名称和角色属性的滚动".这就是"self.intelligence"的来源.

I'm using wxPython to create a wizard; the parent class of my classes is the Wizard from wxPython. That wizard will walk a user through the creation of a character, so the Basic Information page (class BasicInfoPage) lets the user give the character's name and "roll" for the character's attributes. That's where the "self.intelligence" comes from.

我试图在向导中进一步使用页面创建的属性,用户可以在其中选择角色的特长.可用的专业取决于角色具有的属性,例如如果智力足够高,则角色可以是Intel Anaylst.

I'm trying to use the attributes created her for a page further on in the wizard, where the user selects the speciality of the character. The specialities that are available depend on the attributes the character has, e.g. if the intelligence is high enough, the character can be an Intel Anaylst.

我编写程序已经有好几年了,尤其是关于OOP的想法.这就是为什么我对如何使用类和方法创建本质上是全局变量感到困惑的原因.

It's been several years since I've programmed, especially with OOP ideas. That's why I'm confused on how to create what's essentially a global variable with classes and methods.

推荐答案

您可能混淆了类"和实例".从您的示例尚不清楚,因此我假设您使用的是许多类定义,并且没有这些类的适当对象实例.

You may have "Class" and "Instance" confused. It's not clear from your example, so I'll presume that you're using a lot of class definitions and don't have appropriate object instances of those classes.

类实际上没有可用的属性值.类只是对象集合的一组常见定义.您应该将类​​视为定义,而不是实际的东西.

Classes don't really have usable attribute values. A class is just a common set of definitions for a collection of objects. You should think of of classes as definitions, not actual things.

类的实例对象"是具有实际属性值并执行方法功能的实际事物.

Instances of classes, "objects", are actual things that have actual attribute values and execute method functions.

您不要在之间传递变量.您在 instances 之间传递变量.实际上,仅实例变量很重要. [是的,有类变量,但是它们是相当专业的,并且常常令人困惑,最好避免.]

You don't pass variables among classes. You pass variables among instances. As a practical matter only instance variables matter. [Yes, there are class variables, but they're a fairly specialized and often confusing thing, best avoided.]

创建对象(类的实例)时

When you create an object (an instance of a class)

b= BasicInfoPage(...)

然后b.intelligenceBasicInfoPageb实例的智能值.

Then b.intelligence is the value of intelligence for the b instance of BasicInfoPage.

一个真正常见的事情是

class MOS( wx.wizard.PageSimple ):
    def __init__( self, parent, title, basicInfoPage ):
        <snip>
        self.basicInfo= basicInfoPage

现在,在MOS方法中,您可以说self.basicInfo.intelligence,因为MOS具有可用于BasicInfoPage的对象.

Now, within MOS methods, you can say self.basicInfo.intelligence because MOS has an object that's a BasicInfoPage available to it.

构建MOS时,为它提供应该使用的BasicInfoPage实例.

When you build MOS, you provide it with the instance of BasicInfoPage that it's supposed to use.

someBasicInfoPage= BasicInfoPage( ... ) 
m= MOS( ..., someBasicInfoPage )

现在,对象m可以检查someBasicInfoPage.intelligence

这篇关于类之间的Python传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆