我想知道将以下JavaScript代码移植到Java的最有效方法是什么?
// Copies a variable number of methods from source to target.
rebind = function(target, source) {
var z = 1, c = arguments.length, func;
while (++z < c) {
target[func = arguments[z]] = rebind(target, source, source[func]);
}
return target;
};
// method is a getter-setter:
// If passed with no arguments, gets the value.
// If passed with arguments, sets the value and returns target.
function rebind(target, source, func) {
return function() {
var value = func.apply(source, arguments);
return value === source ? target : value;
};
}
Run Code Online (Sandbox Code Playgroud)
你没有.
Java不像JavaScript那样动态; 你"不能"将方法从一个地方复制到另一个地方.
有一些方法可以假装你可以做到这一点,例如通过实现一个可以委派的对象,通过实现静态方法的接口(仅限JDK8 +),破坏IDE功能的可怕字节码游戏,运行时类加载冒险等等.上.
除了在有限的,可怕的情况下,你在编译或加载时都这样做,而不是运行时.
| 归档时间: |
|
| 查看次数: |
53 次 |
| 最近记录: |