Scala — class declared fields and access modifiers

前端 未结 2 1267
无人共我
无人共我 2020-12-19 06:57

I have been experimenting with Manifests and such in Scala, and I am having a very hard finding a way to use an object\'s fields when accessed via the getDeclaredFields meth

相关标签:
2条回答
  • 2020-12-19 07:24
    class Woah(val x: String, val y: String) {
      def printParams = classOf[Woah].getDeclaredFields foreach { field =>
        field.setAccessible(true)
        println(field.get(this))
      }
    }
    
    0 讨论(0)
  • 2020-12-19 07:40

    All JVM fields in Scala are private. Access to them is made only through getters, in Scala, so that there's no distinction between them and a parameterless method.

    0 讨论(0)
提交回复
热议问题