如何使用Rjb访问嵌套的静态类?

编程入门 行业动态 更新时间:2024-10-27 00:25:29
本文介绍了如何使用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...)

webcache.googleusercontent/search?q=缓存:1p7OdptgsYUJ:blog.voneicken/2006/12/3/accessing-inner-java-classes-via-rjb+inner+class+rjb+ruby+java& CD = 10安培; HL = EN&安培; CT = clnk&安培; gl = au

我无法抗拒调查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访问嵌套的静态类?

本文发布于:2023-11-08 11:45:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569280.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   如何使用   静态   Rjb

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!