1·protected : 受保护的。它的特点有如下4个:
(1) 在类的内部可以访问。
(2) 在类的外部不能访问。
(3) 在伴生对象中可以访问。
(4) 在子类中可以访问
2·private:私有的
(1)在类的内部可以访问。
(2)在 类的外部不能访问。
(3) 在伴生对象中可以访问。
3·private[this]。更加严格的私有权限。
(1) 在类的内部可以访问。
(2) 在类的外部不能访问 private[this]。
(3) 在伴生对象不能访问 private[this]。
(4) 在子类中不能访问 private[this]。
object w49 {
object Person {
def test(p:Person): Unit={
println(p.password)
}
}
class Person() {
var name: String = "xx";
protected var birthday: String = "2000-10-10"
private var password:String="123456"
private [this] var money:Int =100
def t():Unit ={
println(money)
}
}
class Son extends Person() {
println(birthday)
}
def main(args: Array[String]): Unit = {
val p1 = new Person()
println(p1.name)
new Son()
Person.test(p1)
}
}