在类之间传递变量
我有两个类对相同的变量进行操作,但彼此相反。有没有一种方法可以在它们之间传输变量,而无需对每次交换进行硬编码或将所有值组合到一个数组中?合并这些类不是一个选择。
#pseudocode
class class1:
def __init___(self):
# code
initialize variable MAIN
# do stuff
# do stuff
make variable stuff
# do stuff
make variable thing
# do stuff
class class2:
def __init___(self):
# code
initialize variable stuff
initialize variable thing
# do stuff
# do stuff
undo variable stuff
# do stuff
undo variable thing
# do stuff
make variable MAIN
我希望能够在 class1
和 class2
之间快速来回发送数据。
I have 2 classes that operate on the same variables but do the inverse of each other. Is there a way to transfer the variables between them without hard coding every exchange or combining all the values into one array? Combining the classes is not an option.
#pseudocode
class class1:
def __init___(self):
# code
initialize variable MAIN
# do stuff
# do stuff
make variable stuff
# do stuff
make variable thing
# do stuff
class class2:
def __init___(self):
# code
initialize variable stuff
initialize variable thing
# do stuff
# do stuff
undo variable stuff
# do stuff
undo variable thing
# do stuff
make variable MAIN
I want to be able to send data back and forth between class1
to class2
quickly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将共享数据放入第三个对象并从两个类引用它。
Put the shared data into a third object and reference it from both classes.
我认为这比你想象的要容易得多。以下是在两个类之间“发送”数据的一些示例。
用法如下:
I think this is much easier than you think. Here is some examples of "sending" data between two classes.
Usage like this: