}
=============================================================================================================================
package org.cma.scala.scope
object HigherPrivate {
def main(args: Array[String]): Unit = {
val higherScope = new HigherScope()
higherScope.useInCmaMethod
}
}
class HigherScope{
/**
* private[cma] 控制了方法的可用范围:
* 1 org.cma package中的元素可以使用该方法
* 2 org.cma 子包(包含递归子包)中的元素可以使用该方法
*/
private[cma] def useInCmaMethod = {
println("you can use this method if you are in cma or the sub package")
}
}
===================================================================================================
package com.scala.idle
object ProtectedObj {
def main(args: Array[String]): Unit = {
val protectedContainer = new SubProtectedContainer()
protectedContainer.showXXX
}
}
class ProtectedContainer{
/**
* 这个方法是可以被子类调用的,这是private method 做不到的。
*/
protected def protectedMethod = {
println("This is a protected method of class ProtectedContainer")
}
}
class SubProtectedContainer extends ProtectedContainer{
def showXXX = {
//调用父类的方法
super.protectedMethod
}
}
scala private private[package] protected 使用说明
原文地址:http://blog.csdn.net/hi_1234567/article/details/43155811