使用 Javascript Rhino 重载 Java 方法

发布于 2024-12-09 15:10:02 字数 218 浏览 4 评论 0原文

我有一个类,如下所示,我想使用 Javascript Rhino 覆盖这两种方法,有什么建议吗?

提前致谢!!

class MyClass() {
  public void method1(); 
  public void method2(); 

  MyClass() {
    method1();
    method2();
  } 
}

I have a class such the one below and I would like to override both methods using Javascript Rhino, any suggestions?

Thanks in advance!!

class MyClass() {
  public void method1(); 
  public void method2(); 

  MyClass() {
    method1();
    method2();
  } 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

把人绕傻吧 2024-12-16 15:10:02

上次我使用它时,JDK 捆绑的 Rhino 不支持扩展类(仅实现 SAM 接口),但 Mozilla 的 Rhino 确实支持重写类。查看 JavaAdapterhttps ://developer.mozilla.org/en-US/docs/Mozilla/Projects/Rhino/Scripting_Java#The_JavaAdapter_Constructor

The last time I used it, the JDK bundled Rhino didn't support extending classes (only implementing SAM interfaces), but Mozilla's Rhino does support overriding classes. Check out the JavaAdapter: https://developer.mozilla.org/en-US/docs/Mozilla/Projects/Rhino/Scripting_Java#The_JavaAdapter_Constructor

忆梦 2024-12-16 15:10:02

虽然我没有在 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);

While I've not tried this in an Android environment, Rhino does support a JavaAdapter construct - as seen at: https://developer.mozilla.org/en-US/docs/Mozilla/Projects/Rhino/Scripting_Java#The_JavaAdapter_Constructor which allows you to create a JS object defining the correct methods, and then to create effectively a wrapper between a single superclass, and/or one or more interfaces. In this case, something like:

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文