 |
Forums |
Admin Discussion Forums: help Start New Thread
By: arton Tajima
RE: Extending Java Class For Real [ reply ] 2012-03-09 13:50
|
Hi,
If you have already defined 'interface' in Java world, you can create pseud java object by ruby or extend it using delegation.
below is the conceptual codes.
package com.example;
public interface Foo {
String foo();
}
public class FooImpl implements Foo {
public String foo() {
return "FooImpl";
}
}
class FooUser {
void useFoo(Foo f) {
System.out.prinln("hello " + f.foo());
}
}
------------------------------
class FooExt
def initialize(jf)
@javaobj = jf
end
def foo()
"#{@javaobj.foo()} from ruby world"
end
end
##
JavaFoo = Rjb::import('com.example.FooImpl')
foo = FooExt.new(JavaFoo.new)
foo = Rjb::bind(foo, 'com.example.Foo')
FooUser = Rjb::import('com.example.FooUser')
foouser = FooUser.new
foouser.useFoo(foo) #=> Hello FooImpl from ruby world
|
By: Victor Savkin
Extending Java Class For Real [ reply ] 2012-03-09 12:56
|
Hi,
I have a java class that I need to extend. I cannot use any Ruby tricks (such as instance_eval) to do it because I need to create an instance of that class and pass it to a java library. Basically, I need to use that extended class in Java. Is there a way to do it with RJB?
Thanks,
Victor
|
|
 |