[英]How to make a anonymous class with void return type method and no parameters into a lambda expression?
[英]How to return String in anonymous class for a method returning void
我有點困惑。 我有以下幾點:
public static String showInputDialog() {
Form frm = new Form();
final Command cmd = new Command("Ok");
final TextField txt = new TextField("Enter the text", null, 1024, 0);
frm.addCommand(cmd);
frm.append(txt);
frm.setCommandListener(new CommandListener() {
public void commandAction(Command c, Displayable d) {
if (c == cmd) {
return txt.getString(); // Error !!
} else {
return null; // Error !!
}
}
});
}
如您所見,我想返回輸入對話框字符串,而匿名類方法應返回void。 我該如何解決這個問題?
這不符合您的預期。
我看到已經有一些解決方案,但是我覺得對實際發生的事情進行更多的討論可能會有所幫助。
當您調用frm.setCommandListener(new CommandListener() { ... })
,代碼向用戶顯示一個對話框,用戶可以在其中鍵入一些文本並提交,但是代碼不會停止並等待用戶完成。 相反,代碼將繼續執行-不會產生結果。 只有在用戶完成輸入並提交之后,您才會被叫回以處理結果-這可能會在以后發生,或者根本沒有發生。
我猜您有一些代碼調用此方法,例如:
public void someMethod(int foo, String bar) {
[...]
String result = MyInputForm.showInputDialog();
// do something with the result
System.out.println("hey, got a result "+ result);
[...]
}
相反,您需要重新組織它。 首先編寫一個幫助程序類來處理結果:
公共靜態類MyCallBack {
public MyCallBack(... /* here pass in what you need to process the result*/) {
... remember necessary stuff in instance variables
}
public void processResult(String result) {
// do something with the result
System.out.println("hey, got a result "+ result);
[...]
}
}
然后調用方只做:
public void someMethod(int foo, String bar) {
[...]
MyInputForm.showInputDialog( new MyCallBack(... here pass in stuff ...) );
[...]
}
並且實際代碼必須更改為:
public static String showInputDialog(final MyCallBack callback) {
Form frm = new Form();
final Command cmd = new Command("Ok");
final TextField txt = new TextField("Enter the text", null, 1024, 0);
frm.addCommand(cmd);
frm.append(txt);
frm.setCommandListener(new CommandListener() {
public void commandAction(Command c, Displayable d) {
if (c == cmd) {
return callback.processResult(txt.getString());
} else {
return; // or just omit the else part
}
}
});
}
兩個問題:
CommandListener
之外,我還需要定義第二個幫助器類。 那真的不是好風格。 我希望可以對其進行改進,但是由於我看不到完整的代碼(反正這將是太多的信息),因此我不得不把它留給您來改進代碼並擺脫混亂。 雖然我覺得您希望有一個模塊化的,可重復使用的輸入對話框助手,但這可能不是最佳方法。 最好直接在需要結果的地方定義Form
, TextField
和Command
並使其運行。 運行它之后,請使其在第二步中可重用。 假設CommandListener是固定的 ,則兩個可能的選項是
在外部類中使用類成員變量並分配給該變量
private static String myText;
...
public static String showInputDialog() {
...
frm.setCommandListener(new CommandListener() {
public void commandAction(Command c, Displayable d) {
if (c == cmd) {
myText = txt.getString();
} else {
myText = null;
}
}
});
}
或創建CommandListener
的具體實現並將返回值設置為新實現的屬性
我將看看使此代碼段中的方法/變量為非靜態 ...
如果您對String進行某些操作或將其存儲在某個位置,則無需返回它,例如:
static String result;
public String commandAction(Command c, Displayable d) {
if (c == cmd) {
result = txt.getString();
} else {
result = null;
}
}
雖然您會遇到線程問題要處理。
您無法返回字符串,因為您不知道何時調用監聽器。 一旦有了字符串,就可以使用它來做一些事情。
public static void showInputDialog() {
StringHandler sh = new StringHandler();
frm.setCommandListener(new CommandListener() {
public void commandAction(Command c, Displayable d) {
if (c == cmd) {
sh.handle(txt.getString());
} else {
sh.handle(null);
}
}
});}
public class StringHandler {
public void handle(String s){
// Do something with that string.
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.