一.访问权限控制
- 公共的,不添加修饰符,在任何地方都可以访问
- protected:受保护的,在类的外部不能访问,在子类中访问
- private:私有的,在类的外部都不能访问,在子类中不能访问。在伴生对象中可以访问
- private [this]只能被当前对象中使用,在类的内部使用。
object Person {
def test(p: Person): Unit = {
println(p.password)
}
}
class Person() {
var name: String = "***"
protected var birthday: String = "2000-10-10"
private var password: String = "12345"
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)
}