Python 我将如何从一个类访问变量到另一个类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19993795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 15:12:00  来源:igfitidea点击:

How would I access variables from one class to another?

pythonclassvariables

提问by user2714543

I am writing a program that is utilizing multiple classes. I have one class that is dedicated to determining values for a set of variables. I would then like to be able to access the values of those variables with other classes. My code looks as follows:

我正在编写一个使用多个类的程序。我有一个专门用于确定一组变量的值的类。然后我希望能够使用其他类访问这些变量的值。我的代码如下所示:

class ClassA(object):
    def __init__(self):
        self.var1 = 1
        self.var2 = 2

    def methodA(self):
        self.var1 = self.var1 + self.var2
        return self.var1


class ClassB(ClassA):
    def __init__(self):
        self.var1 = ?
        self.var2 = ?

object1 = ClassA()
sum = object1.methodA()
print sum

I use classA to initialize 2 variables (var1 and var2). I then use methodA to add them, saving the result as var1 (I think this will make var1 = 3 and var2 = 2). What I want to know is how would I have ClassB then be able to get the values for var1 and var2 from ClassA?

我使用 classA 来初始化 2 个变量(var1 和 var2)。然后我使用methodA添加它们,将结果保存为var1(我认为这将使var1 = 3和var2 = 2)。我想知道的是我如何让 ClassB 然后能够从 ClassA 获取 var1 和 var2 的值?

采纳答案by Steinar Lima

var1and var2are instance variables. That means that you have to send the instance of ClassAto ClassBin order for ClassB to access it, i.e:

var1并且var2实例变量。这意味着,你必须发送的实例ClassAClassB为了ClassB的访问它,即:

class ClassA(object):
    def __init__(self):
        self.var1 = 1
        self.var2 = 2

    def methodA(self):
        self.var1 = self.var1 + self.var2
        return self.var1



class ClassB(ClassA):
    def __init__(self, class_a):
        self.var1 = class_a.var1
        self.var2 = class_a.var2

object1 = ClassA()
sum = object1.methodA()
object2 = ClassB(object1)
print sum

On the other hand - if you were to use class variables, you could access var1 and var2 without sending object1 as a parameter to ClassB.

另一方面 - 如果您要使用类变量,则可以访问 var1 和 var2,而无需将 object1 作为参数发送给 ClassB。

class ClassA(object):
    var1 = 0
    var2 = 0
    def __init__(self):
        ClassA.var1 = 1
        ClassA.var2 = 2

    def methodA(self):
        ClassA.var1 = ClassA.var1 + ClassA.var2
        return ClassA.var1



class ClassB(ClassA):
    def __init__(self):
        print ClassA.var1
        print ClassA.var2

object1 = ClassA()
sum = object1.methodA()
object2 = ClassB()
print sum

Note, however, that class variables are shared among all instances of its class.

但是请注意,类变量在其类的所有实例之间共享。

回答by mgilson

Can you explain whyyou want to do this?

你能解释一下你为什么要这样做吗?

You're playing around with instance variables/attributeswhich won't migrate from one class to another (they're bound not even to ClassA, but to a particular instanceof ClassAthat you created when you wrote ClassA()). If you want to have changes in one class show up in another, you canuse class variables:

你玩弄实例变量/属性,不会从一类迁移到另一个(他们势必甚至没有ClassA,而是一个特定的实例ClassA,你创建的,当你写的ClassA())。如果你想让一个类中的更改显示在另一个类中,你可以使用类变量:

class ClassA(object):
   var1 = 1
   var2 = 2
   @classmethod
   def method(cls):
       cls.var1 = cls.var1 + cls.var2
       return cls.var1

In this scenario, ClassBwill pick up the values on ClassAfrom inheritance. You can then access the class variables via ClassA.var1, ClassB.var1or even from an instance ClassA().var1(provided that you haven't added an instance method var1which will be resolved before the class variable in attribute lookup.

在这种情况下,将从继承中ClassB获取值ClassA。然后,您可以通过ClassA.var1ClassB.var1甚至从实例访问类变量ClassA().var1(前提是您没有添加var1将在属性查找中的类变量之前解析的实例方法。

I'd have to know a little bit more about your particular use case before I know if this is a course of action that I would actually recommend though...

在我知道这是否是我真正推荐的行动方案之前,我必须更多地了解您的特定用例......

回答by Hymanzhai

class ClassA(object):
    def __init__(self):
        self.var1 = 1
        self.var2 = 2
    def method(self):
        self.var1 = self.var1 + self.var2
        return self.var1

class ClassB(ClassA):
    def __init__(self):
        ClassA.__init__(self)

object1 = ClassA() 
sum = object1.method()  
object2 = ClassB() 
print sum

回答by Manu Pandu

we can access/passing arguments/variables from one class to another class using object reference.

我们可以使用对象引用从一个类访问/传递参数/变量到另一个类。

#Class1
class Test:
    def __init__(self):
        self.a = 10
        self.b = 20
        self.add = 0

def calc(self):
    self.add = self.a+self.b

#Class 2
class Test2:
    def display(self):
        print('adding of two numbers: ',self.add)
#createing object for Class1
obj = Test()
#invoke calc method()
obj.calc()
#pass class1 object to class2
Test2.display(obj)