Scala 访问权限控制——Scala Access Modifiers

来源:互联网 发布:js观察者模式 阮一峰 编辑:程序博客网 时间:2024/06/05 17:24

其他的都和Java的差不多,唯一的区别是多了Scope protect

Scope of protection

Access modifiers in Scala 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. Consider the following example:

package society {   package professional {      class Executive {         private[professional] var workDetails = null         private[society] var friends = null         private[this] var secrets = null         def help(another : Executive) {            println(another.workDetails)            println(another.secrets) //ERROR         }      }   }}

Note the following points from the above example:

  • Variable workDetails will be accessible to any class within the enclosing packageprofessional.

  • Variable friends will be accessible to any class within the enclosing packagesociety.

  • Variable secrets will be accessible only on the implicit object within instance methods (this).


Spark中到处都可以看到 Scope of protection, 例如以下代码段

// Create the Spark execution environment (cache, map output tracker, etc)  private[spark] val env = SparkEnv.create(    conf,    "<driver>",    conf.get("spark.driver.host"),    conf.get("spark.driver.port").toInt,    isDriver = true,    isLocal = isLocal)  SparkEnv.set(env)  // Used to store a URL for each static file/jar together with the file's local timestamp  private[spark] val addedFiles = HashMap[String, Long]()  private[spark] val addedJars = HashMap[String, Long]()  // Keeps track of all persisted RDDs  private[spark] val persistentRdds = new TimeStampedHashMap[Int, RDD[_]]  private[spark] val metadataCleaner =    new MetadataCleaner(MetadataCleanerType.SPARK_CONTEXT, this.cleanup, conf)  // Initialize the Spark UI  private[spark] val ui = new SparkUI(this)




0 0
原创粉丝点击