简体   繁体   中英

Java inner class import to Scala project

I want to import java inner class into Scala project. The code, which wouldn't compile looks like this:

import pac.Obj
import pac.Obj.Inner.Inner2

object Sample {
    def main(args: Array[String]): Unit = {            
        var o = new Obj()
        Inner2 i2 = o.getInner().addInner2("some text")
    }
}

The scala compiler is unable to recognize the second import. Why is that? In Java, this construct works fine.

It does work if the Inner class is static .

If it isn't, well you're out of luck (but do you really need it?). But you can use the name with the # separator like this:

var inner = outer.getInner : Outer#Inner

在任何情况下, Inner2 i2在Scala中都是非法的,并且val i2 = o.getInner().addInner2("some text")可以正常工作。

According to Iulian Dragos, who would know ,

There is indeed no syntax to import Outer#Inner.

( link )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM