2
這種似乎很容易弄清楚,但現在很困惑:如何使用反射獲取默認的構造函數參數?
scala> class B(i:Int)
defined class B
scala> classOf[B].getDeclaredFields
res12: Array[java.lang.reflect.Field] = Array()
注意這一點:
scala> class C(i:Int){
| val j = 3
| val k = -1
| }
defined class C
scala> classOf[C].getDeclaredFields
res15: Array[java.lang.reflect.Field] = Array(private final int C.j, private final int C.k)
啊,明白了!我正在考慮一些設計,然後我想我會在REPL之前嘗試它:)太棒了! – Dragonborn
如果你在類中使用'i',它也會成爲一個字段,比如'class B(i:Int){def age = i}' – Chirlo