直接上代码 注意点写在注释里了
/**
* 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
}
}
}
}