当前位置:网站首页>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()不能访问别的对象的私有成员。
边栏推荐
- The advanced version of the cattle brushing series (search for rotating sorted arrays, inversion of the specified range in the linked list)
- 【私人系列】日常PHP遇到的各种稀奇古怪的问题
- ELK日志分析系统
- Golang logging library zerolog use record
- 刷题记录----字符串
- Mapped Statements collection does not contain value for的解决方法
- 【MindSpore】用coco2017训练Model_zoo上的 yolov4,迭代了两千多batch_size之后报错,大佬们帮忙看看。
- 云数据库和本地数据库有什么区别?
- [Node implements data encryption]
- Cesium加载离线地图和离线地形
猜你喜欢

已删除
![[Node implements data encryption]](/img/8b/f9ff44c39fc9e80b2f2d2626a74076.png)
[Node implements data encryption]

VBA 运行时错误‘-2147217900(80040e14):自动化(Automation)错误

Talking about Contrastive Learning (Contrastive Learning) the first bullet

MySQL database --- Addition, deletion, modification and query of MySQL tables (advanced)

MySQL sub-database sub-table

VS Code connects to SQL Server

MySQL数据库————视图和索引

MindSpore:【MindSpore1.1】Mindspore安装后验证出现cudaSetDevice failed错误

coming!Dongfang Selection brings goods to the live broadcast of Longjiang agricultural products
随机推荐
牛客刷题系列之进阶版(搜索旋转排序数组,链表内指定区间反转)
MySQL数据库之JDBC编程
LeetCode 0952.按公因数计算最大组件大小:建图 / 并查集
【私人系列】日常PHP遇到的各种稀奇古怪的问题
The JDBC programming of the MySQL database
How to copy table structure and table data in MySQL
Zabbix 5.0 Monitoring Tutorial (1)
M3SDA:用于多源域自适应的矩匹配
Talking about Contrastive Learning (Contrastive Learning) the first bullet
MySQL复制表结构、表数据的方法
Golang logging library zerolog use record
MindSpore:【MindSpore1.1】Mindspore安装后验证出现cudaSetDevice failed错误
VS Code connects to SQL Server
VBA批量将Excel数据导入Access数据库
MySQL performance optimization (hardware, system configuration, table structure, SQL statements)
MindSpore: CV.Rescale(rescale,shift)中参数rescale和shift的含义?
跨进程启动后台服务
Install Mysql5.7 under Linux, super detailed and complete tutorial, and cloud mysql connection
How architects grow
Install MySQL tutorial under Linux