当前位置:网站首页>Scala language learning-07-constructor
Scala language learning-07-constructor
2022-07-01 07:52:00 【Sunshine clover lxgzxj】
One 、 grammar
class Class name (){
// primary constructor
def this( Parameters : type ) = {
// there this Is the key word , Represents an auxiliary constructor
// The number of parameters is defined by itself
}
def this( Parameters : type , Parameters : type ) = {
}
}
Two 、 matters needing attention
(1)this Is the key word , Represents an auxiliary constructor .
(2) Auxiliary constructors can have multiple .
(3)Scala The auxiliary constructor is distinguished by the difference of the incoming parameters .
(4) The secondary constructor must call the primary constructor .
(5) Formal parameters are not constructor attributes , Can't change .
3、 ... and 、 Test code
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" // Formal parameters are not constructor attributes , Can't change
NewStudent2.PrintInfo()
}
}
// Nonparametric construction method
class Student(){
var name: String = "Czg"
var age: Int = 18
println("")
println("========================")
println(" The main constructor call of the parameterless constructor !!!")
println(s"name : ${
name} , age : ${
age}")
def this(name: String) = {
this()
this.name = name
println(" An auxiliary constructor call to a parameterless constructor !!!")
println(s"name : ${
name} , age : ${
age}")
}
def PrintInfo(): Unit = {
println("++++++++++++++++++++++++")
println(s"name : ${
name} , age : ${
age}")
}
}
// Parametric construction method
class Student1( var name: String, var age: Int){
println("")
println("========================")
println(" The main constructor of the parameterized constructor calls !!!")
println(s"name : ${
name} , age : ${
age}")
def this(name: String) = {
this(name, 20)
this.name = name
println(" The auxiliary constructor of the parameterized constructor calls !!!")
println(s"name : ${
name} , age : ${
age}")
}
def PrintInfo(): Unit = {
println("++++++++++++++++++++++++")
println(s"name : ${
name} , age : ${
age}")
}
}
// Formal parameter construction method
class Student2(name: String, age: Int){
println("")
println("========================")
println(" Main constructor call with formal parameter constructor !!!")
println(s"name : ${
name} , age : ${
age}")
def this(name: String) = {
this(name, 20)
//this.name = name
println(" Auxiliary constructor call with formal parameter constructor !!!")
println(s"name : ${
name} , age : ${
age}")
}
def PrintInfo(): Unit = {
println("++++++++++++++++++++++++")
println(s"name : ${
name} , age : ${
age}")
}
}
Four 、 test result
========================
The main constructor call of the parameterless constructor !!!
name : Czg , age : 18
========================
The main constructor call of the parameterless constructor !!!
name : Czg , age : 18
An auxiliary constructor call to a parameterless constructor !!!
name : Lxg , age : 18
++++++++++++++++++++++++
name : Zxj , age : 18
========================
The main constructor of the parameterized constructor calls !!!
name : Sun , age : 27
========================
The main constructor of the parameterized constructor calls !!!
name : Lxg , age : 20
The auxiliary constructor of the parameterized constructor calls !!!
name : Lxg , age : 20
++++++++++++++++++++++++
name : Zxj , age : 27
========================
Main constructor call with formal parameter constructor !!!
name : Moon , age : 30
========================
Main constructor call with formal parameter constructor !!!
name : Book , age : 20
Auxiliary constructor call with formal parameter constructor !!!
name : Book , age : 20
++++++++++++++++++++++++
name : Moon , age : 30
Process ended , Exit code 0
边栏推荐
- Scala语言学习-07-构造器
- 凸印的印刷原理及工艺介绍
- [batch dos-cmd command - summary and summary] - Common operators in the CMD window (<, < <, & <,>, > >, & >, & >, & &, ||, (),;, @)
- QT -- 1. QT connection database
- Rk3399 platform development series explanation (network debugging) 7.30. What will affect the sending process of TCP packets?
- matlab保存DB4i深度相机图片
- 2022 mobile crane driver test exercises and online simulation test
- 【批处理DOS-CMD-汇总】扩展变量-延迟变量cmd /v:on、cmd /v:off、setlocal enabledelayedexpansion、DisableDelayedExpansion
- Atguigu---- scaffold --02- use scaffold (2)
- [Shenzhen IO] precise Food Scale (some understanding of assembly language)
猜你喜欢

Browser local storage

论文学习——水文时间序列相似性查询的分析与研究

C # read and write customized config file

2022 test question bank and simulation test of tea master (primary) operation certificate

MySQL and redis consistency solution

ONES 创始人王颖奇对话《财富》(中文版):中国有没有优秀的软件?

源代码加密的意义和措施

组件的自定义事件①

QT -- 1. QT connection database

Custom events of components ②
随机推荐
力扣每日一题-第31天-1790.仅执行一次字符串交换能否使两个字符串相等
【网站架构】一招搞定90%的分布式事务,实打实介绍数据库事务、分布式事务的工作原理应用场景
浏览器本地存储
Warm congratulations on the successful listing of five elements hehe liquor
QT -- 1. QT connection database
[microservice openfeign] feign's log record
C # read and write customized config file
漏刻有时API接口实战开发系列(14):身份证实名鉴权验证
Apple账号密码自动填充
如何使用layui将数据库中的数据以表格的形式展现出来
【mysql学习笔记27】存储过程
[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
STM32 uses esp01s to go to the cloud, mqtt FX debugging
Saving db4i depth camera pictures with MATLAB
redisson使用全解——redisson官方文檔+注釋(上篇)
2022年流动式起重机司机考试练习题及在线模拟考试
TCP/UDP 通信问题整理
Basic number theory -- combinatorial number
[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
力扣每日一题-第32天-1822.数组元素积的符号