2

为什么有些人更喜欢使用

.setCommandListener(this)

超过

.setCommandListener(new CommandListener(){})

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

4

2 回答 2

1

如果使用“this”,则必须实现类的监听器,然后才能在监听器的实现方法中访问类的字段。

如果您使用第二个(新侦听器...),那么如果您不需要访问课堂上的许多其他内容,那么它可能是更具可读性的代码。

于 2011-09-06T04:01:32.513 回答
0

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

看起来初学者程序员只是盲目地从教程中复制这种反模式,而没有额外考虑。

尽管根据我的经验,更复杂的代码setCommandListener(new CommandListener(){/*..*/})更容易维护和阅读。

另请注意,您可以在这两种情况下访问类的字段,只是后者需要使用Qualified this

//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

顺便说一句,我是否提到上述方法也更安全?它保证您期望特定屏幕的侦听器正是您设置的侦听器。

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

    // 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
于 2011-09-06T12:37:30.130 回答