当前位置:网站首页>Scala类中的属性
Scala类中的属性
2022-07-30 19:34:00 【Code Writers】
- 属性的声明方法
1.1 在主构造器形参中声明
class Student (val name: String,var age: Int){
println(“Primary constructor and Auxiliary constructor!”)
def fun = name + " is " + age +" years old!"
}
1
2
3
4
Scala 类的主构造器函数的形参包括三种类型:未用任何修饰、var 修饰、val 修饰:
未用任何修饰符修饰,这个参数就是一个局部变量,但当其被至少一个方法使用时,就变为属性,但该属性只能在类内访问!
var 修饰参数,作为类的成员属性使用,可以修改
val 修饰参数,作为类只读属性使用,不能修改
1.2 在主构造器内部声明
在主构造器内部使用val或者var声明的变量都是类的属性
- 属性的var与val
scala中类的属性默认都是private类型的,但默认会生成public类型的getter()和setter(),即私有属性,公有方法。
scala中没有public关键字,没有修饰符的属性用起来就像public一样,但是其底层是private实现的
在scala中,getter与setter并不是被命名为getXXX和setXXX,而是XXX()和XXX_eq()
在Scala中:
如果一个属性被声明为var且没有任何修饰符, Scala会为该字段生成公有的getter和setter方法。
如果属性是val且没有任何修饰符, Scala生成一个私有的final字段和一个getter方法,但是没有setter。
在主构造器形参中,如果一个属性没有var或者val的修饰符, Scala不会生成getter和setter方法。
object Demo{
def main(args:Array[String]): Unit={
val p = new Person(“xxx”)
println(p.name) //访问不到
p.name = “ddddd” //访问不到
}
class Person(name:String){
}
}
1
2
3
4
5
6
7
8
9
10
11
var和val属性被private关键字修饰时,不会生成getter和setter方法。
3. 访问权限修饰符
private :私有权限,只在类的内部和伴生对象中可用。
protected :受保护权限,Scala 中受保护权限比 Java 中更严格,同类、子类可以访问,同包无法访问
- 作用域保护
Scala中,访问修饰符可以通过使用限定词强调。格式为:
private[x]或protected[x]
1
这里的x指代某个所属的包、类或单例对象。如果写成private[x],读作"这个成员除了对[…]中的类或[…]中的或[this]中的包中的类及它们的伴生对像可见外,对其它所有类都是private。
这里对 private[this]进行说明
class Student{
private var privateAge = 0
def isYounger(other:Student)= privateAge < other.privateAge
//类的方法可以访问这个类的所有对象的私有字段(或私有属性)
//所以isYounger这个方法可以访问Student这个类的other对象的私有属性privateAge
}
1
2
3
4
5
6
class Student{
private[this] var privateAge = 0
def isYounger(other:Student)= privateAge < other.privateAge//报错
}
1
2
3
4
private[this]限定了这个类的方法只能访问自己对象的属性privateAge,不能访问其他对象(即使这个对象是根据这个类生成)的属性。
即这个属性只能是当前对象私有,isYounger()不能访问别的对象的私有成员。
边栏推荐
- win2003下FTP服务器如何搭建
- VS Code connects to SQL Server
- 第十七届“振兴杯”全国青年 职业技能大赛——计算机程序设计员(云计算平台与运维)参赛回顾与总结
- How architects grow
- 阿里面试官:给我描述一下缓存击穿的现象,并说说你的解决思路?
- Listen to the boot broadcast
- el-input 只能输入整数(包括正数、负数、0)或者只能输入整数(包括正数、负数、0)和小数
- MindSpore:【模型训练】【mindinsight】timeline的时间和实际用时相差很远
- Download and installation of the latest version of MySQL 8.0 under Linux (detailed steps)
- VBA batch import Excel data into Access database
猜你喜欢
Zabbix 5.0 监控教程(一)
云数据库和本地数据库有什么区别?
MySQL database - views and indexes
Cesium加载离线地图和离线地形
Typora设置标题自动标号
MindSpore:【MindSpore1.1】Mindspore安装后验证出现cudaSetDevice failed错误
【MindSpore】用coco2017训练Model_zoo上的 yolov4,迭代了两千多batch_size之后报错,大佬们帮忙看看。
M3SDA:用于多源域自适应的矩匹配
Win11如何更改默认下载路径?Win11更改默认下载路径的方法
阿里面试官:给我描述一下缓存击穿的现象,并说说你的解决思路?
随机推荐
MySQL database - DQL data query language
【flink】报错整理 Could not instantiate the executor. Make sure a planner module is on the classpath
The advanced version of the Niu Ke brushing series (team competition, sorting subsequences, inverting strings, deleting common characters, repairing pastures)
055 c# print
MySQL数据库 ---MySQL表的增删改查(进阶)
Talking about Contrastive Learning (Contrastive Learning) the first bullet
Install Mysql5.7 under Linux, super detailed and complete tutorial, and cloud mysql connection
【PyTorchVideo教程01】快速实现视频动作识别
在华为云,见证迷你世界的神奇觉醒
MySQL mass production of data
Start background services across processes
Interviewer Ali: Describe to me the phenomenon of cache breakdown, and talk about your solution?
VBA runtime error '-2147217900 (80040e14): Automation error
浅聊对比学习(Contrastive Learning)第一弹
MindSpore:【resnet_thor模型】尝试运行resnet_thor时报Could not convert to
Entering the applet for the first time
【科普】无线电波怎样传送信息?
MySQL复制表结构、表数据的方法
VS Code connects to SQL Server
HCIP --- 企业网的三层架构