本文介绍了如何使用Rjb访问嵌套的静态类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说Java程序定义了A类,它有一个嵌套的静态类'B'。

Lets say a Java program defines the class A, which has a nested static class 'B'.

如何使用Ruby-访问B类是怎样的? Java Bridge?

How is it possible to access class B using the Ruby-Java Bridge?

例如,这些尝试不起作用:

For example, these attempts do not work:

A = Rjb::import('package.A')
A.B 
A::B

有没有办法实现这个目标?

Is there a way to accomplish this?

推荐答案

Google从2006年开始缓存此结果。但听起来很合理,所以接受它并进行实验!

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

(PS:我是java + ruby​​用户,但从未使用过Rjb,所以只是传递信息......)

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

我无法抗拒调查Les访问静态内容的问题,我想我找到了语法。访问内部类(静态或非静态)看起来有点不可思议,但它是可行的。静态加载像任何其他类一样,但它们的路径名是'OuterClass $ StaticInnerClass'。非静态内部类别有点棘手。像静态一样导入'OuterClass $ Inner';现在你有了内部类,但诀窍在于实例化一个实例:你必须提供一个OuterClass实例作为构造函数的第一个参数(因此在java的幕后揭示内部对其外部方法的隐式访问)数据):

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

这篇关于如何使用Rjb访问嵌套的静态类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 06:36