在Nashorn中使用重载方法实现java类型

在Nashorn中使用重载方法实现java类型,java,nashorn,Java,Nashorn,有没有办法在Nashorn中用两种方法的单独代码实现这个接口 public interface Test { void methodA(int a); long methodA(long b); } 因为这样做将实现两种方法: var x = new whatever.Test { methodA: function (a) { print(a + ", type: " + (typeof a)); // type is number, so I don

有没有办法在Nashorn中用两种方法的单独代码实现这个接口

public interface Test {
    void methodA(int a);
    long methodA(long b);
}
因为这样做将实现两种方法:

var x = new whatever.Test {
    methodA: function (a) {
        print(a + ", type: " + (typeof a)); // type is number, so I don't even know what method was actually called
    }
};
不,没有

由于JavaScript没有重载方法的概念,所有Java重载将共享相同的JavaScript函数。如果您需要不同的行为,那么您必须尝试根据参数的数量和类型来区分自己。如果你有


方法A(对象x)
methodA(对象x、对象y)

你可以


函数方法A(x,y)

您可以检测到,例如,
typeof(y)=“undefined”
,然后调用单个arg版本。或者,您可以在整个过程中使用
参数
数组


然而,在您的例子中,由于JavaScript只有一种数字类型,没有单独的整数类型,int和long都变成了数字(float和double、byte和short也是如此),因此,除了测试值范围之外,实际上没有任何方法来判断调用了哪个重载。

可以选择从nashorm调用选定的java方法,比如
x['methodA(long)”(43)
可能还有一些方法来设置该方法的处理程序?还是在实施时?除非有人能找到办法,否则我将在下周将这个答案标记为有效。