开发者

Handling private fields in Scala traits when implementing them in Java

开发者 https://www.devze.com 2023-03-11 05:33 出处:网络
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 wh

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消