Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 设置命令侦听器样式首选项_Java_Java Me_Midp_Lcdui - Fatal编程技术网

Java 设置命令侦听器样式首选项

Java 设置命令侦听器样式首选项,java,java-me,midp,lcdui,Java,Java Me,Midp,Lcdui,为什么有些人更喜欢使用 .setCommandListener(this) 结束 更多的时候?在什么情况下我应该使用第二个,为什么? 我假设这只是一个风格问题,还是有一个特殊的问题?如果使用“this”,则必须将侦听器实现到类,然后才能访问侦听器的实现方法中的类字段 如果您使用第二个(新的侦听器…),那么如果您不需要访问类中的许多其他内容,那么它的代码可能更可读。setCommandListener(此)在“玩具代码”中明显更容易阅读。我想这就是为什么我在许多入门级教程中看到了它的使用,在这些

为什么有些人更喜欢使用

.setCommandListener(this)
结束

更多的时候?在什么情况下我应该使用第二个,为什么? 我假设这只是一个风格问题,还是有一个特殊的问题?

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

如果您使用第二个(新的侦听器…),那么如果您不需要访问类中的许多其他内容,那么它的代码可能更可读。

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

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

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

还请注意,在这两种情况下,仅后者需要使用:

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

比方说,如果您直接重写setCommandListener(此)并运行它,您会注意到奇怪的行为-命令“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
//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
    // 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