繁体   English   中英

如何为返回void的方法在匿名类中返回String

[英]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之外,我还需要定义第二个帮助器类。 那真的不是好风格。 我希望可以对其进行改进,但是由于我看不到完整的代码(反正这将是太多的信息),因此我不得不把它留给您来改进代码并摆脱混乱。 虽然我觉得您希望有一个模块化的,可重复使用的输入对话框助手,但这可能不是最佳方法。 最好直接在需要结果的地方定义FormTextFieldCommand并使其运行。 运行它之后,请使其在第二步中可重用。

假设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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM