当前位置:网站首页>Scala语言学习-07-构造器
Scala语言学习-07-构造器
2022-07-01 07:48:00 【阳光九叶草LXGZXJ】
一、语法
class 类名(){
//主构造器
def this(参数:类型) = {
//这里的this是关键字,表示辅助构造器
//参数的个数自己定义
}
def this(参数:类型,参数:类型) = {
}
}
二、注意事项
(1)this是关键字,表示辅助构造器。
(2)辅助构造器可以有多个。
(3)Scala是通过传入参数的不同来区分是哪个辅助构造器的。
(4)辅助构造器必须调用主构造器。
(5)形参不是构造方法属性,不能更改。
三、测试代码
object TestClassInitMethod {
def main(args: Array[String]): Unit = {
var NewStudent: Student = new Student()
var NewStudent_1: Student = new Student("Lxg")
NewStudent.name = "Zxj"
NewStudent.PrintInfo()
var NewStudent1: Student1 = new Student1("Sun", 27)
var NewStudent1_1: Student1 = new Student1("Lxg")
NewStudent1.name = "Zxj"
NewStudent1.PrintInfo()
var NewStudent2: Student2 = new Student2("Moon", 30)
var NewStudent2_1: Student2 = new Student2("Book")
//NewStudent2.name = "Zxj" //形参不是构造方法属性,不能更改
NewStudent2.PrintInfo()
}
}
//无参构造方法
class Student(){
var name: String = "Czg"
var age: Int = 18
println("")
println("========================")
println("无参构造方法的主构造器调用!!!")
println(s"name : ${
name} , age : ${
age}")
def this(name: String) = {
this()
this.name = name
println("无参构造方法的辅助构造器调用!!!")
println(s"name : ${
name} , age : ${
age}")
}
def PrintInfo(): Unit = {
println("++++++++++++++++++++++++")
println(s"name : ${
name} , age : ${
age}")
}
}
//有参构造方法
class Student1( var name: String, var age: Int){
println("")
println("========================")
println("有参构造方法的主构造器调用!!!")
println(s"name : ${
name} , age : ${
age}")
def this(name: String) = {
this(name, 20)
this.name = name
println("有参构造方法的辅助构造器调用!!!")
println(s"name : ${
name} , age : ${
age}")
}
def PrintInfo(): Unit = {
println("++++++++++++++++++++++++")
println(s"name : ${
name} , age : ${
age}")
}
}
//形参构造方法
class Student2(name: String, age: Int){
println("")
println("========================")
println("有形参构造方法的主构造器调用!!!")
println(s"name : ${
name} , age : ${
age}")
def this(name: String) = {
this(name, 20)
//this.name = name
println("有形参构造方法的辅助构造器调用!!!")
println(s"name : ${
name} , age : ${
age}")
}
def PrintInfo(): Unit = {
println("++++++++++++++++++++++++")
println(s"name : ${
name} , age : ${
age}")
}
}
四、测试结果
========================
无参构造方法的主构造器调用!!!
name : Czg , age : 18
========================
无参构造方法的主构造器调用!!!
name : Czg , age : 18
无参构造方法的辅助构造器调用!!!
name : Lxg , age : 18
++++++++++++++++++++++++
name : Zxj , age : 18
========================
有参构造方法的主构造器调用!!!
name : Sun , age : 27
========================
有参构造方法的主构造器调用!!!
name : Lxg , age : 20
有参构造方法的辅助构造器调用!!!
name : Lxg , age : 20
++++++++++++++++++++++++
name : Zxj , age : 27
========================
有形参构造方法的主构造器调用!!!
name : Moon , age : 30
========================
有形参构造方法的主构造器调用!!!
name : Book , age : 20
有形参构造方法的辅助构造器调用!!!
name : Book , age : 20
++++++++++++++++++++++++
name : Moon , age : 30
进程已结束,退出代码0
边栏推荐
- TCP/UDP 通信问题整理
- Jax's deep learning and scientific computing
- 2022年流动式起重机司机考试练习题及在线模拟考试
- 组件的自定义事件①
- ctfshow-web355,356(SSRF)
- Detailed explanation of weback5 basic configuration
- Introduction to kubernetes resource objects and common commands (II)
- Will Internet talents be scarce in the future? Which technology directions are popular?
- The triode is a great invention
- Discussion on several research hotspots of cvpr2022
猜你喜欢

C# Newtonsoft.Json中JObject的使用

如何让两融交易更极速

Apple account password auto fill

How to create an exclusive vs Code theme

The computer has a network, but all browser pages can't be opened. What's the matter?

微软宣布开源 (GODEL) 语言模型聊天机器人

【编程强训2】排序子序列+倒置字符串

She is the "HR of others" | ones character

H5 页面设置了字体的粗细样式,但是在华为手机里微信打开访问样式不生效?

LeetCode+ 71 - 75
随机推荐
2022 mobile crane driver test exercises and online simulation test
【微服务|openfeign】Feign的日志记录
Redisson uses the full solution - redisson official document + comments (Part 2)
JAX的深度学习和科学计算
[programming compulsory training 3] find the longest consecutive number string in the string + the number that appears more than half of the times in the array
atguigu----脚手架--02-使用脚手架(2)
继妹变继母,儿子与自己断绝关系,世界首富马斯克,为何这么惨?
Redisson utilise la solution complète - redisson Documents officiels + commentaires (Partie 1)
Huawei modelarts training alexnet model
Caesar
[R language] age sex frequency matching select samples for case-control study, and perform frequency matching on age and sex
Ctfhub port scan (SSRF)
【mysql学习笔记28】存储函数
【R语言】两个/N个数据合并merge函数
Discussion on several research hotspots of cvpr2022
H5 页面设置了字体的粗细样式,但是在华为手机里微信打开访问样式不生效?
Redisson uses the full solution - redisson official documents + comments (Part 2)
【mysql学习笔记26】视图
Is it safe and reliable for Huatai Securities to open an account? How to open Huatai Securities Account
[skill] create Bat quick open web page