Java 脚本引擎类实例化
我正在尝试在我们的应用程序中实现脚本端点,但有一个问题: 是否可以实例化一个具有实现方法的抽象类实例的对象?在 Java 中,它看起来像:
SimpleObject obj = new SimpleObject("contructor arg0") {
public void doCustomAction() {
System.out.println("Action");
}
}
SimpleObject
是一个具有抽象方法 doCustomAction
的抽象类 如何通过 Java 脚本引擎来做这样的事情?我正在尝试按以下方式执行操作:
obj1 = new SimpleObject("value1") {
doCustomAction : function() {
//Do smth.
}
}
但是引擎抛出这样的异常:
javax.script.ScriptException: sun.org.mozilla.javascript.internal.EvaluatorException:
error instantiating (JavaAdapter: first arg should be interface Class (<Unknown source>#1)):
class SimpleObject is interface or abstract (<Unknown source>#1) in <Unknown source> at line number 1
I'm trying to implement scripting endpoint in our application, but I have a problem:
Is it possible to instantiate an object that is instance of abstract class with implemented methods? In Java it will look like:
SimpleObject obj = new SimpleObject("contructor arg0") {
public void doCustomAction() {
System.out.println("Action");
}
}
SimpleObject
is an abstract class with abstract method doCustomAction
How to do such thing via Java Scripting Engine? I'm trying to do in the following way:
obj1 = new SimpleObject("value1") {
doCustomAction : function() {
//Do smth.
}
}
But engine throws such exception:
javax.script.ScriptException: sun.org.mozilla.javascript.internal.EvaluatorException:
error instantiating (JavaAdapter: first arg should be interface Class (<Unknown source>#1)):
class SimpleObject is interface or abstract (<Unknown source>#1) in <Unknown source> at line number 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以用 Java 扩展抽象类,例如 MouseAdapter 使用它的接口,然后在 javascript 中使用这个“适配器”,例如:
在 Java 中:
在 JavaScript 中:
编辑:
您可以在没有“适配器”的情况下做到这一点,只需执行以下操作:
You could extend your abstract class in Java, something like MouseAdapter does with its interfaces, then in javascript you use this "adapter", like:
In Java:
In JavaScript:
Edit:
You could do it without the "adapter", just do: