当前位置:网站首页>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
边栏推荐
- Stepsister becomes stepmother, son breaks off relationship with himself, and musk, the world's richest man, why is it so miserable?
- Subclasses call methods and properties of the parent class with the same name
- base64
- Download xshell and xftp
- Oracle创建自增id
- 长路漫漫、技术作伴
- 论文学习——水文时间序列相似性查询的分析与研究
- 【mysql学习笔记26】视图
- 华为ModelArts训练Alexnet模型
- [R language] age sex frequency matching select samples for case-control study, and perform frequency matching on age and sex
猜你喜欢
![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

关系数据库如何工作

IMDB practice of emotion classification (simplernn, LSTM, Gru)

Microsoft announces open source (Godel) language model chat robot

【编程强训】删除公共字符(哈希映射)+组队竞赛(贪心)

Gui Gui programming (XV) - use scale to control font size changes

2022 test questions and mock examinations for main principals of hazardous chemicals business units

【编程强训3】字符串中找出连续最长的数字串+数组中出现次数超过一半的数字
![[kv260] generate chip temperature curve with xadc](/img/fc/e5e4648b09b1123b2d494b75a9f8f7.png)
[kv260] generate chip temperature curve with xadc

C# Newtonsoft. Use of job in JSON
随机推荐
weback5基础配置详解
浏览器本地存储
2022 Guangdong Provincial Safety Officer a certificate third batch (main person in charge) special operation certificate examination question bank simulated examination platform operation
[chapter 72 of the flutter problem series] a solution to the problem that pictures taken in the flutter using the camera plug-in are stretched
redisson使用全解——redisson官方文档+注释(下篇)
Download xshell and xftp
ctfshow-web351(SSRF)
奥迪AUDI EDI 项目中供应商需要了解哪些信息?
2022制冷与空调设备运行操作国家题库模拟考试平台操作
良心安利万向轮 SolidWorks模型素材网站
Félicitations pour l'inscription réussie de wuxinghe
【无标题】
Kdtree notes
How to create an exclusive vs Code theme
【R语言】两个/N个数据合并merge函数
TodoList经典案例①
Saving db4i depth camera pictures with MATLAB
2022 operation of refrigeration and air conditioning equipment operation of national question bank simulated examination platform
Why some people earn nearly 10billion a year, while others earn 3000 a month: the details you ignore actually make the most money
Illusory and simple screen raindrop post-processing effect