gpt4 book ai didi

java - 等待字符串值从 null 更改为其他值

转载 作者:太空宇宙 更新时间:2023-11-04 14:10:48 25 4
gpt4 key购买 nike

我有一个像下面这样的类(class)

public class Myclass {
private static String mystring = null;
public String process() {
output = null; // reset the value
try {
mystring = longprocess();
} catch (Exception e) {}
return mystring;
}
}

问题是我的字符串方法“process”总是得到空值

如何等待 mystring 的值从 null 更改为其他值,然后才返回该值?

注意:必须仅在方法过程中返回变量“mystring”,而不能在 return 语句本身中使用 longprocess。

最佳答案

根据您向pastebin提供的代码(顺便说一句,为什么不将其包含在问题中?),看来您在分配实际值之前返回了output字符串。

方法 RootShell.getShell(true).add(cmd) 将异步执行提供的命令,因此您无法在命令的回调内分配方法的局部变量 output(可以,但实际赋值将在 return output; 语句之后发生,因此您将始终从 longprocess() 方法返回 null)。

作为一种解决方法,我建议您将某种监听器传递给 longprocess() 方法,并从此监听器分配 mystring 变量。

关于java - 等待字符串值从 null 更改为其他值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28345589/

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