Handling private fields in Scala traits when implementing them in Java
When implementing a Scala trait, one has to implement the trait's methods and the simply call the respective static method on Foo$class.class
. I'm not sure however how to deal with private fields which are defined in the trait.
That is, if there is a field _something
in the trait, if I don't implement _something_$eq()
and _something()
, the compiler yells at me. My assumption is that I have to create a _something
field in my implementing class.
I just want to confirm that this is the corre开发者_Python百科ct way of handling this issue.
Yes, create the private field in the class
Note you also need to call the init method of the Foo$class module
If you want to see exactly what you need to do, just create a Scala class that extends Foo, compile it, and then use a decompiler (jd-gui) to see how it was generated.
精彩评论