Given the case class, what will be the result of calling method inside the object accessing private field of case class?
class Person(val name: String, private val superheroName: String) //The superhero name is private!

object Person {
  def showMeInnerSecret(x: Person) = x.superheroName
}

val clark = new Person("Clark Kent", "Superman")
val peter = new Person("Peter Parker", "Spider-Man")

Person.showMeInnerSecret(clark)
Person.showMeInnerSecret(peter)
Explanation
A companion object can also see private values and variables of the corresponding classes' instantiated objects.

Source: Scala Exercises: objects

Следи за CodeGalaxy

Мобильное приложение Beta

Get it on Google Play
Обратная Связь
Продолжайте изучать
тесты по Scala
Cosmo
Зарегистрируйся сейчас
или Подпишись на будущие тесты