📌  相关文章
📜  从另一个类 python 访问类变量 - 任何代码示例

📅  最后修改于: 2022-03-11 14:59:50.805000             🧑  作者: Mango

代码示例1
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