(几周前刚开始使用 Scala,请耐心等待)
在这里阅读/试用这篇小文章,遇到了一些惊喜
http://danielwestheide.com/blog/2012/12/19/the-neophytes-guide-to-scala-part-5-the-option-type.html
按照描述定义案例类用户后:
case class User(
id: Int,
firstName: String,
lastName: String,
age: Int,
gender: Option[String])
object UserRepository {
private val users = Map(1 -> User(1, "John", "Doe", 32, Some("male")),
2 -> User(2, "Johanna", "Doe", 30, None))
def findById(id: Int): Option[User] = users.get(id)
def findAll = users.values
}
,这是我观察到的结果:
> scala> UserRepository.findById(1)
> res34: Option[User] = Some(User(1,John,Doe,32,Some(male)))
>
> scala> UserRepository.findById(1).get
> res35: User = User(1,John,Doe,32,Some(male))
>
> scala> UserRepository.findById(1).getOrElse("N/A")
> res36: java.io.Serializable = User(1,John,Doe,32,Some(male))
>
> scala> UserRepository.findById(3).getOrElse("N/A")
> res37: java.io.Serializable = N/A
前两个是我所期望的,但后两个不是;现有用户和非现有用户的情况。为什么突然出现 java.io.Serializable?