我一直将案例类的构造函数参数理解为定义公共 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