1. 程式人生 > >Scala學習筆記(四)----private的訪問許可權

Scala學習筆記(四)----private的訪問許可權

直接上程式碼 注意點寫在註釋裡了
/**
  * Scala 對private關鍵字進行的細粒度訪問控制
  * 和java不一樣的在與protected關鍵字和private關鍵字,Scala預設的關鍵字是public
  * Scala支援巢狀包定義
  * 如果需要對別的包可見的話,可以寫成private[cn],private[limbo]等
  * import語句可以寫在任意的位置
  */

package cn.limbo.demo

package society{

  package professional{
    class Executive {
      private [professional] var workDetails = null;
      private [society] var friends = null
      private [this] var secrets = null     //this表示只能在該例項下才可以訪問該屬性

      def help(another : Executive){
        println(another.workDetails)
       // println(another.secrets)  // error
      }
    }
  }
  package social {
    class Acquaintance() {
      def socialize(person:professional.Executive): Unit ={
        println(person.friends)  // ok
       // println(person.workDetails) // error
      }
    }
  }
}