设置命令监听器样式首选项的疑问

时间:2011-09-06 03:56:49

标签: java java-me midp lcdui

为什么有些人喜欢使用

.setCommandListener(this)

.setCommandListener(new CommandListener(){})

更频繁?我应该在什么情况下使用第二个?为什么? 我假设它只是风格问题还是存在特定问题?

2 个答案:

答案 0 :(得分:1)

如果使用“this”,则必须实现该类的侦听器,然后可以在已实现的侦听器方法中访问类的字段。

如果您使用第二个(新的Listener ...),那么如果您不需要访问班级中的许多其他内容,它可能是更易读的代码。

答案 1 :(得分:0)

setCommandListener(this)在“玩具代码”中更容易阅读。我认为这就是为什么我看到它在许多入门级教程中使用,其中作者不想太深入。

它看起来像初学者程序员只是盲目地从教程中复制这个反模式,而不给它额外的想法。

对于更复杂的代码,虽然setCommandListener(new CommandListener(){/*..*/})在我的经验中更容易维护和阅读。

另请注意,you can access fields of class在这两种情况下,只需要Qualified this使用{{3}}:

//import javax.microedition.midlet.*;
//import javax.microedition.lcdui.*;

abstract class ListenerTest extends MIDlet {
    protected Display display;

    protected void startApp() {
        display = Display.getDisplay(this);
        Form form = new Form("welcome");
        form.addCommand(new Command("go", Command.OK, 1));
        form.setCommandListener(new CommandListener() {
            public void commandAction(Command c, Displayable d) {
                // qualified this - see JLS 15.8.4
                ListenerTest.this.cmdAction(c, d);
            }
        });
        // display "welcome" screen with "go" command
        display.setCurrent(form);
    }

    protected void pauseApp() { }

    protected void destroyApp(boolean unconditional) {
        notifyDestroyed();
    }

    protected abstract void displayNext();

    private void cmdAction(Command c, Displayable d) {
        // invoke from listener to display next screen
        displayNext();
    }
} // ListenerTest

class NextTest extends ScreenTest {

    protected void displayNext() {
        Form form = new Form("bye-bye");
        form.addCommand(new Command("EXIT", Command.EXIT, 1));
        form.setCommandListener(new CommandListener() {
            public void commandAction(Command c, Displayable d) {
                notifyDestroyed();
            }
        });
        // display "bye-bye" screen with "exit" command
        display.setCurrent(form);
    }
} // NextTest
BTW我提到上面的方法也更安全吗?它保证您对特定屏幕所期望的监听器正是您设置的监听器。

说,如果你直接重写 setCommandListener(this)并运行它,你会发现奇怪的行为 - 命令“go”现在将退出midlet而不是显示下一个屏幕:< / p>

    // don't do that
    abstract class ListenerTest extends MIDlet implements CommandListener {
        protected Display display;

        protected void startApp() {
            display = Display.getDisplay(this);
            Form form = new Form("welcome");
            form.addCommand(new Command("go", Command.OK, 1));
            form.setCommandListener(this);
            // display "welcome" screen with "go" command
            display.setCurrent(form);
        }

        protected void pauseApp() { }

        protected void destroyApp(boolean unconditional) {
            notifyDestroyed();
        }

        protected abstract void displayNext();

        public void commandAction(Command c, Displayable d) {
            // invoke from listener... really??? check the subclass
            displayNext();
        }
    } // ListenerTest

    class NextTest extends ScreenTest implements CommandListener {

        protected void displayNext() {
            Form form = new Form("bye-bye");
            form.addCommand(new Command("EXIT", Command.EXIT, 1));
            form.setCommandListener(this);
            // display "bye-bye" screen with "exit" command
            display.setCurrent(form);
        }

        public void commandAction(Command c, Displayable d) {
            // you may not notice but...
            notifyDestroyed();
            // ...this actually overrides superclass implementation
        }
    } // NextTest
相关问题