Access modifiers

Access modifiers can be augmented with qualifiers. A modifier of the form private[X] or protected[X] means that access is private or protected "up to" X, where X designates some enclosing package, class or singleton object.
package quizful {
   package prof {
      class Executive {
         private[prof] var details = null
         private[quizful] var friends = null
         private[this] var secrets = null

         def help(another : Executive) {
            println(another.details)
            println(another.secrets) //ERROR
         }
      }
   }
}
Note the following points:
  • Variable details will be accessible to any class within the enclosing package prof.
  • Variable friends will be accessible to any class within the enclosing package quizful.
  • Variable secrets will be accessible only on the implicit object within instance methods (this).

Следи за CodeGalaxy

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

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