scala入门初级代码训练-16类包伴生对象等的访问权限
原创文章,转载请注明出处!
原文地址: http://www.ptbird.cn/2016/07/19/scala-chuji-demo-16/
There I am,in the world more exciting!
by postbird
package com.ptbird.scala
/**
* Created by postbird on 2016/5/25.
*/
/**
*
* 访问权限 : 类 包 成员 对象 伴生对象
*
**/
/******************************在包的访问权限************************************************/
package spark {
package navigation {
private[spark] class Navigator {
//可以属于 spark的class ,不仅仅只是在navigation的包
// 可见度可以在spark中所有的包和对象使用,不仅仅在navigator中使用
protected[navigation] def useStartChart() {}
//useStartChart这个函数只是属于navigation的包中,
// 但是navigation的子类和包里面的所有的成员都可以使用
class LegOfJourney {
private[Navigator] val distance = 100
//对Navigator里面的所有的属性和方法都是课间的
}
private[this] var speed = 200
//规定只能由当前的对象才能访问自己,属于当前对象私有
}
}
package launch {
import navigation._
object Vehicle {
private[launch] val guide = new Navigator
}
}
}
/******************************在伴生对象中的的访问权限************************************************/
class PackageOps_Advanced{
import PackageOps_Advanced.power
//将伴生对象的私有成员引入进来之后可以直接使用
//伴生类可以直接使用伴生对象
private def canMakeItTrue:Boolean={
power > 10001
}
}
//伴生对象本来就可以直接使用伴生类的私有成员
object PackageOps_Advanced{
private def power=10000
def makeItTrue(p:PackageOps_Advanced):Boolean={
val result=p.canMakeItTrue
result
}
}
object ClassTest5 {
def main(args: Array[String]): Unit = {
}
}
文章版权:Postbird-There I am , in the world more exciting!
本文链接:http://www.ptbird.cn/scala-chuji-demo-16.html
转载请注明文章原始出处 !
扫描二维码,在手机阅读!