通过“精细"访问内部类方法.使用Scala类和Java的Java语法目的 [英] Access inner class method with "fine" syntax in Java using Scala class & object
本文介绍了通过“精细"访问内部类方法.使用Scala类和Java的Java语法目的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下scala示例:
I have the following scala example:
class Nested {}
object Nested {
class Inner {}
object Inner {
def x = 321
}
}
使用JUnit测试专门测试我可以执行Nested.Inner.x()
并在没有Nested.Inner$.MODULE$.x()
的情况下自由调用该方法:
With a JUnit test to test specifically that I can do Nested.Inner.x()
and call the method freely without the Nested.Inner$.MODULE$.x()
:
import static junit.framework.Assert.*;
import org.junit.Test;
public class TestFromJava {
@Test
public void accessTest() {
assertEquals(321, Nested.Inner.x());
}
}
这可以编译,但是在运行测试的那一刻,我得到了(sbt test
):
This compiles, but at the moment of running the test, I am getting (sbt test
):
[error] TestFromJava.java:8: cannot find symbol
[error] symbol: method x()
[error] location: class Nested.Inner
[error] Nested.Inner.x
如何从Scala语法和不使用可怕的$中受益?也许这是该语言在生成object
实例时的一种功能.
How can I benefit from both Scala syntax and not using the horrible $? Maybe it is a feature of the language as how it generates the object
instances.
推荐答案
You have to qualify singleton object
access when calling from Java, as explained in this question.
在您的情况下
Nested.Inner$.MODULE$.x()
这篇关于通过“精细"访问内部类方法.使用Scala类和Java的Java语法目的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文