まず、MyInterface intrf = ConcreteClass.new
は有効なRubyではありません。 MyInterface
は、参照の型指定子ではなく、定数(クラスへの定数参照など)ですが、RubyではJRubyが動的に型指定されています。
第2に、JavaインタフェースMyInterface
を実装するJRubyクラスConcreteClass
を作成したいとします。ここでは、Javaパッケージ 'com.example'にあります。
require 'java'
java_import 'com.example.MyInterface'
class ConcreteClass
# Including a Java interface is the JRuby equivalent of Java's 'implements'
include MyInterface
# You now need to define methods which are equivalent to all of
# the methods that the interface demands.
# For example, let's say your interface defines a method
#
# void someMethod(String someValue)
#
# You could implements this and map it to the interface method as
# follows. Think of this as like an annotation on the Ruby method
# that tells the JRuby run-time which Java method it should be
# associated with.
java_signature 'void someMethod(java.lang.String)'
def some_method(some_value)
# Do something with some_value
end
# Implement the other interface methods...
end
# You can now instantiate an object which implements the Java interface
my_interface = ConcreteClass.new
は、ページ
JRuby Reference特に、詳細は
JRuby wikiを参照してください。