简体   繁体   中英

How a val value bean changed

I use scala with mybatis:

Space.scala:

case class Space(
              val id: Long ,
              spaceName:String)

SpaceDao:

   val space=Space(0,"space name")
   val mapper = session.getMapper(classOf[SpaceMapper])
   val result = mapper.insertSpace(space)
   println(space.id)

The result space id is the auto increment number of mysql, not 0. but both space and space.id is val, why a val value can be changed?

This can be achieved via reflection (which is used by SetFieldInvoker ):

Reflection is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique and can enable applications to perform operations which would otherwise be impossible.

Also see this question .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM