简体   繁体   English

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

[英]How would I access variables from one class to another?

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).我使用 classA 来初始化 2 个变量(var1 和 var2)。 I then use methodA to add them, saving the result as var1 (I think this will make var1 = 3 and var2 = 2).然后我使用 methodA 添加它们,将结果保存为 var1(我认为这将使 var1 = 3 和 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?我想知道的是,我如何让 ClassB 能够从 ClassA 获取 var1 和 var2 的值?

var1 and var2 are instance variables . var1var2实例变量 That means that you have to send the instance of ClassA to ClassB in order for ClassB to access it, ie:这意味着您必须将ClassA的实例发送到ClassB才能让 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.但是请注意,类变量在其类的所有实例之间共享。

Can you explain why you want to do this?你能解释一下你为什么要这样做吗?

You're playing around with instance variables/attributes which won't migrate from one class to another (they're bound not even to ClassA , but to a particular instance of ClassA that you created when you wrote ClassA() ).您正在使用不会从一个类迁移到另一个类的实例变量/属性(它们甚至不绑定到ClassA ,而是绑定到您在编写ClassA()时创建的ClassA的特定实例)。 If you want to have changes in one class show up in another, you can use class variables:如果你想让一个类中的更改显示在另一个类中,你可以使用类变量:

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

In this scenario, ClassB will pick up the values on ClassA from inheritance.在这种情况下, ClassB将从继承中获取ClassA上的值。 You can then access the class variables via ClassA.var1 , ClassB.var1 or even from an instance ClassA().var1 (provided that you haven't added an instance method var1 which will be resolved before the class variable in attribute lookup.然后,您可以通过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...在我知道这是否是我真正推荐的行动方案之前,我必须更多地了解您的特定用例......

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

we can access/pass 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)
#creating object for Class1
obj = Test()
#invoking calc method()
obj.calc()
#passing class1 object to class2
Test2.display(obj)

var1 and var2 is an Instance variables of ClassA . var1 和 var2 是ClassA的实例变量。 Create an Instance of ClassB and when calling the methodA it will check the methodA in Child class ( ClassB ) first, If methodA is not present in ClassB you need to invoke the ClassA by using the super() method which will get you all the methods implemented in ClassA .创建一个 ClassB 实例,当调用 methodA 时,它将首先检查 Child 类( ClassB )中的 methodA,如果ClassB 中不存在methodA,则您需要使用super() 方法调用ClassA ,这将为您提供所有方法在ClassA 中实现。 Now, you can access all the methods and attributes of ClassB.现在,您可以访问 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):
        super().__init__()
        print("var1",self.var1)
        print("var2",self.var2)


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

Just create the variables in a class.只需在类中创建变量。 And then inherit from that class to access its variables.然后从该类继承以访问其变量。 But before accessing them, the parent class has to be called to initiate the variables.但是在访问它们之前,必须调用父类来启动变量。

class a:
    def func1(self):
        a.var1 = "Stack "

class b:
    def func2(self):
        b.var2 = "Overflow"

class c(a,b):
    def func3(self):
        c.var3 = a.var1 + b.var2
        print(c.var3)

a().func1()
b().func2()
c().func3()

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM