python/pygame, 传递输入从一个类到另一个类

0 投票
1 回答
928 浏览
提问于 2025-04-17 03:25

有一种方法可以在一个类和另一个类之间传递一个值或者变量,而不需要通过主函数来传递。

我在使用Python语言。

1 个回答

1

当然,你可以在一个特定对象的方法里访问其他对象的属性。例如:

class A(object):
    def method(self, other):
         other.somevar = 5

class B(object):
    pass

def main():
    a = A()
    b = B()

    b.somevar = "Hello World"
    a.method(b)
    print(b.somevar) # now prints '5'

撰写回答