使用Javascript Rhino重载Java方法

3
我有一个类如下所示,我想使用Javascript Rhino覆盖这两个方法,有什么建议吗?
感谢您的帮助!
class MyClass() {
  public void method1(); 
  public void method2(); 

  MyClass() {
    method1();
    method2();
  } 
}
2个回答

2

1

虽然我没有在Android环境中尝试过这个,但Rhino确实支持JavaAdapter构造 - 如https://developer.mozilla.org/en-US/docs/Mozilla/Projects/Rhino/Scripting_Java#The_JavaAdapter_Constructor所示,它允许您创建一个JS对象来定义正确的方法,然后创建一个包装器,将单个超类和/或一个或多个接口组合起来。在这种情况下,可以使用以下代码:

var o = {
  method1: function() {
    print('method1');
  },
  method2: function() {
    print('method2');
  }
}

//This line should instantiate the child class, with
//method1 + method2 overridden called within the constructor
var instanceThatIsAMyClass = new JavaAdapter(com.example.MyClass, o);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接