Scala的访问权限

33 阅读4分钟

(一)、scala的访问权限概述

Scala中的属性、方法可以通过访问控制符来设置不同的访问权限。不同的访问控制符可以决定是否可以被外部类访问。

有四种访问控制权限,分别为:

(1)默认访问权限。Scala中的默认访问权限相当于Java中的public,Scala中如果一个变量没有任何修饰符,就代表默认访问权限。作用域:全部

(2)protected访问权限。作用域:本类,子类

(3)private访问权限。作用域:类内部(伴生对象:定义方法传入类的对象)

(4)private[this]访问权限。

(二)、protected访问权限

protected : 受保护的。它的特点有如下4个:

(1) 在类的内部可以访问。

(2) 在类的外部不能访问。

(3) 在伴生对象中可以访问。

(4) 在子类中可以访问,与之对应的是:private修饰的,在子类中无法访问

object class24 {

  //name 属性具有默认的访问权限
  class Student(var name:String) {

  }


  def main(args: Array[String]): Unit = {
    val s1 = new Student("小花")
    // 类的外部可以访问
    println(s1.name)
    // protected属性,在类的外部无法访问
    // println(s1.gender)
  }

}

(三)、private访问权限

private : 私有的。它的特点有如下三个。

(1) 在类的内部可以访问。

(2) 在类的外部不能访问。

(3) 在伴生对象中可以访问。

/**
 *
 * 访问权限控制
 *
 * 1.默认访问权限
 * 2.使用protected(被保护的) 修饰符
 *       (1) 在类的外部,不能直接通过对象.属性的方式来访问
 *       (2) 在子类中,可以访问protected 修饰的成员
 * 3.private (私有的)
 *       (1) 在子类中,无法访问。
 */

object class24 {

  //name 属性具有默认的访问权限
  class Father() {
    var name1:String = "小花"

    protected var gender:String = "女"
    protected def test():Unit = {
      println("test")
    }
  }

  // 在子类中,我们通关super 来访问父类
  class Son extends Father{
    // protected 修饰的成员,可以在子类中访问
    super.test()
    println(gender)
    // private 修饰的成员,在子类中不可访问
    // super.test1()
    // println(age)
  }

  def main(args: Array[String]): Unit = {
    var Son1 = new Son()

  }

}
/**
 *
 * 访问权限控制
 *
 * 1.默认访问权限
 * 2.使用protected(被保护的) 修饰符
 *       (1) 在类的外部,不能直接通过对象.属性的方式来访问
 *       (2) 在子类中,可以访问protected 修饰的成员
 * 3.private (私有的)
 *       (1) 在子类中,无法访问。
 *       (2) 在伴生对象,伴生类中可以访问
 */

object class25 {

  class Student() {
    private var age:Int = 10
  }
 object Student {
   def test(stu:Student):Unit = {
    //在伴生对象中,可以通过对象.属性的方法来访问
     println(stu.age)
   }
 }

  def main(args: Array[String]): Unit = {
    var stu = new Student()
    // private 成员,不能在类的外部,通过对象.属性的方式来访问。
    // 报错: stu.age
    Student.test(stu)

  }

}

(四)、private[this]控制方法作用域

private [this] : 更加严格的私有权限。这个属性(方法)只能在调用它的同一个实例内使用。它的特点如下:

(1) 在类的内部可以访问。

(2) 在类的外部不能访问 private[this]。

(3) 在伴生对象不能访问 private[this]。

(4) 在子类中不能访问 private[this]。

/**
 *
 * 访问权限控制
 *
 * 1.默认访问权限
 * 2.使用protected(被保护的) 修饰符
 *       (1) 在类的外部,不能直接通过对象.属性的方式来访问
 *       (2) 在子类中,可以访问protected 修饰的成员
 * 3.private (私有的)
 *       (1) 在子类中,无法访问。
 *       (2) 在伴生对象,伴生类中可以访问
 * 4.private[this]私有,只能在当前的类中访问
 *   子类无法访问,伴生对象、伴生类中不可以访问
 */

object class26 {

  class Student() {
    private var age:Int = 10

    private [this] var secret:String = "xx日,xxx事"

    def test():Unit = {
      // if() {}
      println(secret)
    }
  }
 object Student {
   def test(stu:Student):Unit = {
    //在伴生对象中,可以通过对象.属性的方法来访问
     println(stu.age)
     //private[this]只能在当前的类中访问
     //println(stu.secret)
   }
 }

  def main(args: Array[String]): Unit = {
    var stu = new Student()
    // stu.test()
    // private 成员,不能在类的外部,通过对象.属性的方式来访问。
    // 报错: stu.age
    Student.test(stu)

  }

}

更加严格的私有权限,表示该字段或方法只能在调用它的同一个实例内部可见。这意味着即使在同一类的其他实例中,也无法访问该字段或方法。不能在伴生对象中访问,不能在函数参数中访问。