在类和实例之间传递值 [英] Passing values between class and instance

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

问题描述

  class MyClassA(object):
def __init __(self):
entry = input(Insert a value :::)
b = MyClassB(entry)#将变量条目传递给类MyClassB
c = MyClassC()#Initializied MyClassC准备好接收值p
self.x = bf#从MyClassB获取值f
print(self.x)
self.x1 = cp#从MyClassC获取值f
print(self.x1)

class MyClassB ):
def __init __(self,M):
self.f = M * 10#f将包含(来自MyClassA * 10的值条目)
c = MyClassC(self.f)将变量f传递给类MyClassC

class MyClassC(object):
def __init __(self,passedVar):
self.p = passedVar + 0.1#p将包含值条目从MyClassB + 0.1)

h = MyClassA()

我的示例代码,首先它将要求一个值,并将其存储到变量名为条目
然后,第二个名为 MyClassB 的类将恢复条目中的值,在处理条目值后,a新的将被存储到名为 f 的新变量,这将被传递到名为 MyClassC 的第三个类,然后也回到第一个类。
最后,第三个类对 f 值进行一些处理,并将其存储到另一个变量 p 中。



这是我的代码在用户运行程序时应该如何工作,这是什么样子:



< pre class =lang-none prettyprint-override> 插入一个值::: 9(假设用户在这里键入9)

因此输出应为:

  90不幸的是,我的问题是从 MyClassC传递值。    myClassA 无法正常工作。 
它说我必须从第一个类传递它的两个参数,但它将在执行第2个类后获得值。
我认为我有初始化实例的问题,有人可以更正我的代码工作方式我描述的方式吗?

解决方案

你所遇到的问题是因为有两个独立的实例 MyClassC 被创建,一个在 MyClassA .__ init __() MyClassB .__ init __()中的单独一个。



修复它的简单方法 - 最好的方法是使另一个实例属性中创建的 MyClassC 实例存储在 MyClassB .__ init __()然后在您要检索 p 的值时引用 对象的属性。



这里是我的意思:

  class MyClassA(object):
def __init __(self):
entry = input(Insert a value :::)
b = MyClassB(entry)#将变量条目传递给类MyClassB
####### c = MyClassC #Initializied MyClassC准备好接收值p
self.x = bf#从MyClassB返回值f
print(self.x)
self.x1 = bcp#To从MyClassB中创建的MyClassC实例获取值p
print(self.x1)

class MyClassB(object):
def __init __(self,M):
self.f = M * 10#f将包含(来自MyClassA * 10的值条目)
self.c = MyClassC(self.f)#传递变量f到类MyClassC和保存实例

class MyClassC(object):
def __init __(self,passedVar):
self.p = passedVar + 0.1#p将包含(来自MyClassB + 0.1的值条目)

h = MyClassA()


class MyClassA(object):
    def __init__(self):
        entry = input("Insert a value ::: ")
        b = MyClassB(entry) #To pass the variable entry to class MyClassB
        c = MyClassC() #Initializied MyClassC to be ready for receive the value p
        self.x = b.f #To get back the value f from MyClassB
        print(self.x)
        self.x1 = c.p #To get back the value f from MyClassC
        print(self.x1)

class MyClassB(object):
    def __init__(self, M):
        self.f = M * 10 # f will contain (the value entry from MyClassA *10)
        c = MyClassC(self.f) #To pass variable f to class MyClassC

class MyClassC(object):
    def __init__(self,passedVar):
        self.p = passedVar + 0.1 # p will contain (the value entry from MyClassB + 0.1)

h = MyClassA()

Above is my sample code, first of all it will ask for a value and store it into variable named entry. Then the second class named MyClassB will recover the value inside entry, after processing the entry value, a new one will be stored into new variable named f which will be passed to third class named MyClassC then also back to first class. Finally the third class does some processing of the f value and stores it into yet another variable p.

That's how my code should work when the user runs the program, and this is what it would look like:

Insert a value ::: 9 (assume the user typed 9 here)

so the output should be:

90
90.1

Unfortunately my problem is that passing the value from MyClassC to myClassA doesn't work. It says I must pass it two argument from the first class but it'a going to get the value after executing the 2st class. I think that I have problem with initializing instances, could someone correct my code to work the way I described?

解决方案

The problem you're having is because there are two independent instances of MyClassC being created, one in MyClassA.__init__() and a separate one in MyClassB.__init__().

The easy way to fix it — not necessarily the best — would be to make MyClassB.__init__() store the MyClassC instance it creates in yet another instance attribute, and then refer to the attribute of that object when you want to retrieve the value of p.

Here's what I mean:

class MyClassA(object):
    def __init__(self):
        entry = input("Insert a value ::: ")
        b = MyClassB(entry)  # To pass the variable entry to class MyClassB
####### c = MyClassC()  # Initializied MyClassC to be ready for receive the value p
        self.x = b.f  # To get back the value f from MyClassB
        print(self.x)
        self.x1 = b.c.p  # To get back the value p from MyClassC instance created in MyClassB
        print(self.x1)

class MyClassB(object):
    def __init__(self, M):
        self.f = M * 10  # f will contain (the value entry from MyClassA *10)
        self.c = MyClassC(self.f)  # Pass variable f to class MyClassC and save instance

class MyClassC(object):
    def __init__(self, passedVar):
        self.p = passedVar + 0.1 # p will contain (the value entry from MyClassB + 0.1)

h = MyClassA()

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

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