jeudi 6 juillet 2017

How to access non-public fields and inheritent fields in scala

I have a piece of code as below

object TestReflection {

  def main(args: Array[String]): Unit = {
    val cls = Class.forName("test.Child")
    cls.getDeclaredField("w")
    cls.getDeclaredField("x")
   cls.getDeclaredField("z")
  }
}

class Parent(val x : Int, val t : Int) {

}
class Child(x : Int, z : Int, val w : Int) extends Parent(x, 0) {

}

Seems that both cls.getDeclaredField("x") and cls.getDeclaredField("z") don't wok. Anyone know how to access them correctly ?

Thanks





Aucun commentaire:

Enregistrer un commentaire