Java 如何在匿名类中为返回void的方法返回字符串

Java 如何在匿名类中为返回void的方法返回字符串,java,Java,我有点困惑。我有以下资料: 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.set

我有点困惑。我有以下资料:

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。如何解决此问题?

鉴于此,有两种可能的选择

在外部类中使用类成员变量&改为指定给该变量

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
的具体实现,并将返回值设置为新实现的属性


我想看看如何使此代码段中的方法/变量成为非静态的…

如果您使用字符串执行某些操作或将其存储在某个位置,则不需要返回它,例如:

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.
    }
}

这不符合您的预期

我看到已经有了一些解决方案,但我觉得更多地讨论一下实际情况可能会有所帮助

调用
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
    之外,我还需要定义第二个helper类。那真是不好的风格。我希望它能得到改进,但由于我没有看到完整的代码(无论如何,这将是太多的信息),我不得不让您来改进代码并消除混乱。虽然我觉得您需要一个模块化的、可重用的输入对话框助手,但这可能不是最好的方法;最好直接在需要结果的地方定义
    表单
    文本字段
    命令
    ,并运行该命令。在运行后的第二步中使其可重用
没有办法做到这一点。除非您是
CommandListener
@madhead
CommandListener
的作者,它是一个内置接口,具有方法
CommandAction
。我无法更改重写方法的签名。您不能这样返回它,因为您不知道commandAction何时运行。在这个方法中,你只是把它当作一个听者。请看问题下面的评论。另外,请展示一些代码来详细说明如何使用类成员变量来解决问题。我想创建一个显示输入对话框的静态方法。如何返回
myText
变量?您不会返回任何值,因为这是
CommandListener
接口的限制。您可以设置类变量。对不起。我希望对
showInputDialog
的调用同步(即,它等待用户输入并将结果返回给调用者)。例如,
String res=showInputDialog()。我如何使用成员变量来实现这一点呢?
CommandListener
必须等待一个事件,sry但不要认为同步执行此操作是最好的方法……我想创建一个显示输入对话框的静态方法。如何返回
result
变量?
String res=showInputDialog()。我如何通过另一个方法调用
handle
?正如@Reimeus和我试图告诉您的那样,同步获取此值是一种糟糕的方法。您需要使用某种回调(正如我们都试图向您展示的那样)。因此,StringHandler不应该在某些方法中调用res=showInputDialog(),而应该在获得输入时执行您需要执行的操作。