gpt4 book ai didi

Java,在对象更改时提醒父级

转载 作者:行者123 更新时间:2023-12-02 08:27:32 26 4
gpt4 key购买 nike

我在 Java 中有以下设置,

public class Main {

// starts sub class
SubClass sub = new SubClass();
sub.start();

// sub will keep running and call method alert() on a specif change
// method alert is void but updates a public variable named status on sub
while(1==1) {

// I should ideally be able to catch/read sub status result here
// how can I do it?
}
}

我是 Java 新手,所以这可能无效,我的方法可能是错误的。既然如此,请为我指明正确的方向。

谢谢。

最佳答案

我假设 SubClass.start() 启动一个新线程,以便父级与子级并行运行。那么主类就可以做一个Object.wait()sub 对象上,SubClass 线程可以执行 Object.notify()sub 对象上。

关于Java,在对象更改时提醒父级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4199949/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com