scala的属性访问权限

scala的属性访问权限有四种:

默认访问权限;protected访问权限;private访问权限;private[this]访问权限

Scala 复制代码
package Test1104
//访问控制权限

//                  类的内部方法   伴生对象中的方法  类的外部(对象,访问)  子类对象,访问   子类方法是否可以访问    另一个对象的属性
//默认权限(pubilc)       可             可              可               可                可                  可
//private               可             可             不可               不可             不可                不可
//protected             可             可             不可               不可              可                 不可
//private[this]         可             不可            不可              不可             不可                 不可

class Student{
  //1.默认权限。类似于java中的public
  val name = "小花"

  //2.私有属性。类的内部。不能继承!
  private val score = 59

  //3.受保护的.可以继承!
  protected val car = "宝马香车"

  def say():Unit ={
    println(name,score,car)
  }
}

object Student{
  def run(obj: Student): Unit = {
    println(obj.name, obj.score, obj.car)
  }
}
//子类
class SubStudent extends Student{
  def test():Unit ={
    println(this.name,this.car)
  }
}

object Test1104 {
  def main(args: Array[String]): Unit = {
    val s = new Student()
    println(s)
    val s1 = new SubStudent()
    println(s1)
  }
}
相关推荐
howard20051 天前
1.6.2 掌握Scala数据结构 - 列表
scala·不可变列表·可变列表
howard20057 天前
1.6.1 掌握Scala数据结构 - 数组
scala·定长数组·变长数组
渣渣盟8 天前
Flink Table API与SQL流数据处理实战
大数据·sql·flink·scala
howard200511 天前
1.5 掌握Scala内建控制结构
scala·内建控制结构
howard200511 天前
1.1.2 Windows上安装Scala
scala·windows版本
allway211 天前
Debian Regular Expressions
运维·debian·scala
、BeYourself13 天前
Scala 字面量
开发语言·后端·scala
、BeYourself21 天前
Scala 数据类型
开发语言·后端·scala