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)
  }
}
相关推荐
howard200511 小时前
1.9 掌握Scala抽象类与特质
scala·抽象类·特质
howard20051 天前
1.8.2 掌握Scala类与对象 - 单例对象与伴生对象
scala·伴生对象·单例对象
howard20053 天前
1.7.1 掌握Scala函数 - 声明Scala函数
scala·声明scala函数·显式声明·隐式声明
howard20055 天前
1.6.4 掌握Scala数据结构 - 元组
scala·元组
howard20055 天前
1.6.5 掌握Scala数据结构 - 集合
scala·集合
蓝眸少年CY6 天前
Scala - 基础教程
开发语言·后端·scala
howard20056 天前
1.6.3 掌握Scala数据结构 - 映射
scala·可变映射·不可变映射
亿牛云爬虫专家7 天前
拒绝代理池雪崩:Scala + Akka 构建高并发的路由分发实战
scala·高并发·爬虫代理·代理ip·隧道代理·akka actor 模型·api代理
渣渣盟9 天前
Flink并行数据源:ClickSource实现详解
flink·scala
渣渣盟9 天前
Flink单流转换算子实战解析
flink·scala