Scala getters and setters in Java class

前端 未结 2 1868
故里飘歌
故里飘歌 2021-01-17 14:13

I would like to create a Java class that follows the Scala setters/getters convention.

I tried following simple class, but it does not work:

public c         


        
2条回答
  •  深忆病人
    2021-01-17 14:24

    I’m afraid you can’t. In Scala, the accessor has to be method with no parameter list, like def a = _a. Writing e.g. def a() = _a in Scala would cause the same error, and there is no way that you can define a method with no parameter list in Java. You may be able to fool the Scala compiler by generating your own ScalaSignature, but that's probably not worth the trouble…

提交回复
热议问题