有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JavaScript匿名函数数组到Java的转换

在没有得到任何好的反馈/帮助之前,我问了一个类似的问题。所以我用更多的信息重写这个问题

下面是一个部分转换为Java的Javascript函数。。差不多完成了。 我知道我不能在Java的数组中使用匿名函数,更不用说匿名函数了,唯一与此功能最接近的是内部类,而不是我想要的

我知道这段代码仍然可以转换成多个函数,而不是使用匿名函数数组,比如if语句或switch(...)的混合

下面的函数也使用递归调用初始化匿名函数的LINECONTROL数组

稍后在这个javscript模拟器中,它是这样使用的

LCDCONTROL = (LCDisOn) ? LINECONTROL : DISPLAYOFFCONTROL;

DISPLAYOFFCONTROL是一个空的匿名函数。 像这样声明

 DISPLAYOFFCONTROL = [function() {}];

从不在initalizeLCDController之外调用LINECONTROL数组,只调用LCDCONTROL对象

像这样

//LCDCONTROL[actualScanLine]();

我只是需要一些建议,如何转换到下面的函数,也许只是一个开端,这就是它

下面是JavaScript中部分翻译的函数->;爪哇

public void initializeLCDController() {
    long a = 0;
    while (a < 154) {
        if (a < 143) {
            LINECONTROL[a] = function () {
                if (LCDTicks < 80) {
                    scanLineMode2();
                } else if (LCDTicks < 252) {
                    scanLineMode3();
                } else if (LCDTicks < 456) {
                    scanLineMode0();
                } else {
                    LCDTicks -= 456;
                    if (STATTracker != 3) {
                        if (STATTracker != 2) {
                            if (STATTracker == 0 && mode2TriggerSTAT) {
                                interruptsRequested |= 2;
                            }
                            incrementScanLineQueue();
                        }
                        if (hdmaRunning) {
                            executeHDMA();
                        }
                        if (mode0TriggerSTAT) {
                            interruptsRequested |= 2;
                        }
                    }
                    actualScanLine = ++memory[65348];
                    if (actualScanLine == memory[65349]) {
                        memory[65345] |= 4;
                        if (LYCMatchTriggerSTAT) {
                            interruptsRequested |= 2;
                        }
                    } else {
                        memory[65345] &= 123;
                    }
                    checkIRQMatching();
                    STATTracker = 0;
                    modeSTAT = 2;
                    LINECONTROL[actualScanLine]();
                }
            }
        } else if (a == 143) {
            LINECONTROL[143] = function () {
                if (LCDTicks < 80) {
                    scanLineMode2();
                } else if (LCDTicks < 252) {
                    scanLineMode3();
                } else if (LCDTicks < 456) {
                    scanLineMode0();
                } else {
                    LCDTicks -= 456;
                    if (STATTracker != 3) {
                        if (STATTracker != 2) {
                            if (STATTracker == 0 && mode2TriggerSTAT) {
                                interruptsRequested |= 2;
                            }
                            incrementScanLineQueue();
                        }
                        if (hdmaRunning) {
                            executeHDMA();
                        }
                        if (mode0TriggerSTAT) {
                            interruptsRequested |= 2;
                        }
                    }
                    actualScanLine = memory[65348] = 144;
                    if (memory[65349] == 144) {
                        memory[65345] |= 4;
                        if (LYCMatchTriggerSTAT) {
                            interruptsRequested |= 2;
                        }
                    } else {
                        memory[65345] &= 123;
                    }
                    STATTracker = 0;
                    modeSTAT = 1;
                    interruptsRequested |= (mode1TriggerSTAT) ? 3 : 1;
                    checkIRQMatching();
                    if (drewBlank == 0) {
                        if (totalLinesPassed < 144 || (totalLinesPassed == 144 && midScanlineOffset > -1)) {
                            graphicsJITVBlank();
                            prepareFrame();
                        }
                    } else {
                        --drewBlank;
                    }
                    LINECONTROL[144]();
                }
            }
        } else if (a < 153) {
            LINECONTROL[a] = function () {
                if (LCDTicks >= 456) {
                    LCDTicks -= 456;
                    actualScanLine = ++memory[65348];
                    if (actualScanLine == memory[65349]) {
                        memory[65345] |= 4;
                        if (LYCMatchTriggerSTAT) {
                            interruptsRequested |= 2;
                            checkIRQMatching();
                        }
                    } else {
                        memory[65345] &= 123;
                    }
                    LINECONTROL[actualScanLine]();
                }
            }
        } else {
            LINECONTROL[153] = function () {
                if (LCDTicks >= 8) {
                    if (STATTracker != 4 && memory[65348] == 153) {
                        memory[65348] = 0;
                        if (memory[65349] == 0) {
                            memory[65345] |= 4;
                            if (LYCMatchTriggerSTAT) {
                                interruptsRequested |= 2;
                                checkIRQMatching();
                            }
                        } else {
                            memory[65345] &= 123;
                        }
                        STATTracker = 4;
                    }
                    if (LCDTicks >= 456) {
                        LCDTicks -= 456;
                        STATTracker = actualScanLine = 0;
                        LINECONTROL[0]();
                    }
                }
            }
        }
        ++a;
    }
}

共 (1) 个答案

  1. # 1 楼答案

    这里有一个聪明的枚举方法。你可以通过做ACTIONS[x].act(this)来使用它。它本质上是一个类型安全的跳台

    编辑:添加了getAction。要使用它,请执行getAction(a).act(this),但此时,最好是丢失枚举并直接从重命名的getAction()发送。从enum中获得的唯一好处是每个enum只有一个实例(只比非静态内部类有好处)和跳转表

    class Foo {
    
        protected enum Action {
            FUNCTION_0 {
                public void act(Foo foo) { ... }
            },
    
            FUNCTION_1 {
                public void act(Foo foo) { ... }
            },
    
            FUNCTION_2 {
                public void act(Foo foo) { ... }
            },
    
            FUNCTION_3 {
                public void act(Foo foo) { ... }
            },
    
            ;
    
            public abstract void act(Foo foo);
        }
    
        protected static final Action[] ACTIONS = new Action[154];
        static {
            Arrays.fill(ACTIONS, 0, 143, Action.FUNCTION_0);
            Arrays.fill(ACTIONS, 143, 144, Action.FUNCTION_1);
            Arrays.fill(ACTIONS, 144, 153, Action.FUNCTION_2);
            Arrays.fill(ACTIONS, 153, 154, Action.FUNCTION_3);
        }
    
            protected Action getAction(int a) {
                if (a < 0) { throw new IllegalArgumentError(); }
                else if (a < 143) { return Action.FUNCTION_0; }
                else if (a < 144) { return Action.FUNCTION_1; }
                else if (a < 153) { return Action.FUNCTION_2; }
                else if (a < 154) { return Action.FUNCTION_3; }
                else { throw new IllegalArgumentError(); }
            }
    }