当前位置:网站首页>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
边栏推荐
- MATLAB之基础知识
- ctfshow-web355,356(SSRF)
- Are there any practical skills for operation and maintenance management
- Illusory and simple screen raindrop post-processing effect
- Alibaba OSS postman invalid according to policy: policy condition failed: ["starts with", "key", "test/"]
- Browser local storage
- Redisson uses the full solution - redisson official document + comments (Part 2)
- Rk3399 platform development series explanation (network debugging) 7.30. What will affect the sending process of TCP packets?
- 组件的自定义事件①
- 038 network security JS
猜你喜欢

I bet on performance and won the CTO of the company. I want to build Devops platform!

ctfshow-web352,353(SSRF)

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

ctfshow-web351(SSRF)
![Those high-frequency written tests and interview questions in [Jianzhi offer & Niuke 101] - linked list](/img/9a/44976b5df5567a7aff315e63569f6a.png)
Those high-frequency written tests and interview questions in [Jianzhi offer & Niuke 101] - linked list

Eigen matrix operation Library

Minecraft 1.16.5模组开发(五十一) 方块实体 (Tile Entity)

Cyclic neural network

Array: question brushing record

Apple account password auto fill
随机推荐
她就是那个「别人家的HR」|ONES 人物
AUTOSAR learning record (1) – ECUM_ Init
Stepsister becomes stepmother, son breaks off relationship with himself, and musk, the world's richest man, why is it so miserable?
长路漫漫、技术作伴
go通用动态重试机制解决方案的实现与封装
下载Xshell和Xftp
Custom events of components ①
Those high-frequency written tests and interview questions in [Jianzhi offer & Niuke 101] - linked list
PWN攻防世界int_overflow
base64
三极管是一项伟大的发明
Eigen matrix operation Library
[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
Redisson uses the full solution - redisson official documents + comments (Part 2)
Introduction to kubernetes resource objects and common commands (II)
Are there any practical skills for operation and maintenance management
Discussion on several research hotspots of cvpr2022
Minecraft 1.16.5模组开发(五十一) 方块实体 (Tile Entity)
Félicitations pour l'inscription réussie de wuxinghe
Cadence OrCAD Capture “网络名”相同,但是未连接或连接错误的解放方案之nodename的用法