[英]Instantiating a non-static Java Inner Class from JRuby
So given the following java class: 所以给出以下java类:
class Outer
{
private int x;
public Outer(int x) { this.x = x; }
public class Inner
{
private int y;
public Inner(int y) { this.y = y; }
public int sum() { return x + y; }
}
}
I can create an instance of the inner class from Java in the following manner: 我可以通过以下方式从Java创建内部类的实例:
Outer o = new Outer(1);
Outer.Inner i = o.new Inner(2);
However, I can't seem how to do the same from JRuby 但是,我似乎无法从JRuby那样做
#!/usr/bin/env jruby
require 'java'
java_import 'Outer'
o = Outer.new(1);
i = o.Inner.new(2); #=> NoMethodError: undefined method `Inner' for #<Outer...>
What's the correct way to do this? 这样做的正确方法是什么?
i = Outer::Inner.new(o,2)
从这个讨论中可以看出,你将不得不做Outer:Inner.new(o, 2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.