python传递变量到类的方法中_类之间的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)

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")

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?

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.

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.

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.

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.

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(...)

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

A really common thing is

class MOS( wx.wizard.PageSimple ):

def __init__( self, parent, title, basicInfoPage ):

self.basicInfo= basicInfoPage

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

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

someBasicInfoPage= BasicInfoPage( ... )

m= MOS( ..., someBasicInfoPage )

Now, the object m can examine someBasicInfoPage.intelligence

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值