How to access nested static classes using Rjb?

故事扮演 提交于 2019-12-04 15:18:59

Google cached this result from 2006. Sounds reasonable though, so take it and experiment!

(PS: I'm a java + ruby user, but never used Rjb, so just passing along the info...)

http://webcache.googleusercontent.com/search?q=cache:1p7OdptgsYUJ:blog.voneicken.com/2006/12/3/accessing-inner-java-classes-via-rjb+inner+class+rjb+ruby+java&cd=10&hl=en&ct=clnk&gl=au

I couldn’t resist investigating the issue Les had with accessing static inners and I think I found the syntax. Accessing inner classes (static or not) can look a little wonky but it is doable. Statics are loaded like any other class, but their pathname is ‘OuterClass$StaticInnerClass’. The nonstatic inner classes are a tiny bit trickier. Import like the static, with ‘OuterClass$Inner’; now you have the inner class, but the trick is in instantiating an instance: you must provide an OuterClass instance as the first argument to the constructor (thus revealing a little behind the curtain of java the implicit access an inner has to its outer’s methods and data):

Outer = Rjb::import(‘Outer’)
Inner = Rjb::import(‘Outer$Inner’)
StaticInner = Rjb::import(‘Outer$StaticInner’)

outer = Outer.new
inner = Inner.new(outer)
staticInner = StaticInner.new
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!