我一直理解case類的構造函數參數作爲定義公共val。爲什麼斯卡拉案例類字段沒有反映爲公共?
但是,當我反映字段時,isPublic方法出現錯誤。任何想法爲什麼?
scala> class Test(val name : String, val num : Int)
defined class Test
scala> import scala.reflect.runtime.universe._
import scala.reflect.runtime.universe._
scala> val tpe = typeOf[Test]
tpe: reflect.runtime.universe.Type = Test
scala> def checkValVisibility(t : Type) = {
| t.members
| .filter(_.isTerm)
| .map(_.asTerm)
| .filter(_.isVal)
| .map(memb => "Val " + memb.name.toString.trim + " is public? " + memb.isPublic)
| .mkString("\n")
| }
checkValVisibility: (t: reflect.runtime.universe.Type)String
scala> checkValVisibility(tpe)
res2: String =
Val num is public? false
Val name is public? false
斯卡拉不自動生成私有實例變量的公共訪問器方法嗎?我不確定實現細節應該通過反思來揭示,但也許看着這些方法會讓你更進一步。 – copumpkin