如何在JRuby中指定Ruby类实现多个java接口
我知道指定Ruby类在JRuby中实现一个java接口的语法,但是我不知道如何指定我们是否希望该类实现多个接口 我尝试的是:如何在JRuby中指定Ruby类实现多个java接口,java,ruby,jruby,Java,Ruby,Jruby,我知道指定Ruby类在JRuby中实现一个java接口的语法,但是我不知道如何指定我们是否希望该类实现多个接口 我尝试的是: class MyClass java_implements 'org.scripthelper.ruby.samples.Script' java_implements 'org.scripthelper.context.ContextListener' 我还尝试: class MyClass java_implements 'org.scripthelper.ruby
class MyClass
java_implements 'org.scripthelper.ruby.samples.Script'
java_implements 'org.scripthelper.context.ContextListener'
我还尝试:
class MyClass
java_implements 'org.scripthelper.ruby.samples.Script', 'org.scripthelper.context.ContextListener'
但是当我试着投下这个球时,我有一个例外
Object o1 = object.toJava(Interface1.class);
Object o2 = object.toJava(ContextListener.class);
但奇怪的是,对于第二个接口,我有以下例外:
rg.jruby.exceptions.TypeError: (TypeError) cannot convert instance of class org.jruby.gen.RubyObject1 to interface org.scripthelper.context.ContextListener
(TypeError) cannot convert instance of class org.jruby.gen.RubyObject1 to interface org.scripthelper.context.ContextListener
我的ContextListener接口具有以下Java代码:
public void init(ScriptContext context);
我的Ruby类如下所示:
require 'java'
import 'org.scripthelper.context.ScriptContext'
import 'org.scripthelper.context.ContextListener'
import 'org.scripthelper.context.DefaultScriptContext'
class ScriptClass
java_implements 'org.scripthelper.context.ContextListener', 'org.scripthelper.ruby.samples.Script'
attr_reader :ctx
def init(context)
@ctx = context
end
def execute()
return 10
end
end
只需在界面中使用普通的旧include:
class脚本类
包括org.scripthelper.context.ContextListener
包括org.scripthelper.ruby.samples.Script
属性读取器:ctx
def init(上下文)
@ctx=上下文
结束
def execute()
返回10
结束
结束