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

时间:2020-12-07 22:22:30

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的值?

3 个解决方案

#1


33  

var1 and var2 are instance variables. That means that you have to send the instance of ClassA to ClassB in order for ClassB to access it, i.e:

var1和var2是实例变量。这意味着您必须将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.

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

#2


5  

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()). If you want to have changes in one class show up in another, you can use 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, ClassB will pick up the values on ClassA from inheritance. 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.

在这种情况下,ClassB将从继承中获取ClassA上的值。然后,您可以通过ClassA.var1,ClassB.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...

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

#3


1  

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

#1


33  

var1 and var2 are instance variables. That means that you have to send the instance of ClassA to ClassB in order for ClassB to access it, i.e:

var1和var2是实例变量。这意味着您必须将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.

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

#2


5  

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()). If you want to have changes in one class show up in another, you can use 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, ClassB will pick up the values on ClassA from inheritance. 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.

在这种情况下,ClassB将从继承中获取ClassA上的值。然后,您可以通过ClassA.var1,ClassB.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...

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

#3


1  

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