在我没有得到任何好的反馈/帮助之前,我问了一个类似的问题。所以我正在用更多信息重写这个问题。
这是一个部分转换为 Java 的 Javascript 函数。它几乎完成了。 我知道我不能在 Java 中的数组中使用匿名函数,更不用说匿名函数了,只有最接近此功能的是内部类,而不是我想要的。
我知道这段代码仍然可以转换成多个函数,而不是使用匿名函数数组,比如 if 语句或 switch(...)
下面的函数也使用递归调用初始化匿名函数的 LINECONTROL 数组。
后面在这个javscript模拟器中是这样使用的
LCDCONTROL = (LCDisOn) ? LINECONTROL : DISPLAYOFFCONTROL;
DISPLAYOFFCONTROL
是一个空的匿名函数。
像这样声明
DISPLAYOFFCONTROL = [function() {}];
你永远不会在 initalizeLCDController
之外调用 LINECONTROL
数组,你只能调用 LCDCONTROL
对象。
像这样
//LCDCONTROL[actualScanLine]();
我只需要一些建议如何转换为下面的功能,也许只是关于它的一个良好开端。
这是从 JavaScript -> Java 部分翻译的函数
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;
}
}
最佳答案
这是一个聪明的枚举方法。您可以通过执行 ACTIONS[x].act(this)
来使用它。它本质上是一个类型安全的跳转表。
编辑:添加了 getAction
。要使用它,请执行 getAction(a).act(this)
,但在这一点上,您最好不要丢失枚举并直接从重命名的 getAction()
。您从枚举中获得的唯一好处是每个枚举只有一个实例(仅优于非静态内部类)和跳转表。
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(); }
}
}
关于JavaScript 匿名函数数组到 Java 的翻译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22602680/