如何在JRuby中指定Ruby类实现了多个Java接口

问题描述 投票:0回答:1

我知道在Ruby中指定一个Ruby类实现一个Java接口的语法,但是我不知道如何指定我们是否希望该类实现多个接口。

我尝试过的是:

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

我知道指定Ruby类在JRuby中实现一个Java接口的语法,但是我不知道如何指定我们是否希望该类实现多个接口。我试过的是:...

java ruby jruby
1个回答
0
投票

只需在界面中使用普通的旧包含:

© www.soinside.com 2019 - 2024. All rights reserved.