当前位置:网站首页>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
边栏推荐
- 2022电工(中级)复训题库及答案
- 继妹变继母,儿子与自己断绝关系,世界首富马斯克,为何这么惨?
- MySQL and redis consistency solution
- Stepsister becomes stepmother, son breaks off relationship with himself, and musk, the world's richest man, why is it so miserable?
- 【R语言】两个/N个数据合并merge函数
- Illusory and simple screen raindrop post-processing effect
- [microservice openfeign] feign's log record
- Array: question brushing record
- Sorting out tcp/udp communication problems
- [kv260] generate chip temperature curve with xadc
猜你喜欢

The H5 page has set the font thickness style, but the wechat access style in Huawei mobile phone doesn't take effect?

浏览器本地存储

关系数据库如何工作

Thesis learning -- Analysis and Research on similarity query of hydrological time series
![[MySQL learning notes 25] SQL statement optimization](/img/01/cf0556961670bb43512caab8d5f4e5.png)
[MySQL learning notes 25] SQL statement optimization

She is the "HR of others" | ones character

The computer has a network, but all browser pages can't be opened. What's the matter?
![[programming training 2] sorting subsequence + inverted string](/img/96/87750c5d3954ef6c39cce073e8b9ae.png)
[programming training 2] sorting subsequence + inverted string

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

How outlook puts together messages with the same discussion
随机推荐
TCP/UDP 通信问题整理
Those high-frequency written tests and interview questions in [Jianzhi offer & Niuke 101] - linked list
Browser local storage
[kv260] generate chip temperature curve with xadc
How do the top ten securities firms open accounts? In addition, is it safe to open a mobile account?
Apple account password auto fill
Oracle创建自增id
[batch dos-cmd command - summary and summary] - Common operators in the CMD window (<, < <, & <,>, > >, & >, & >, & &, ||, (),;, @)
Illusory and simple screen raindrop post-processing effect
Basic number theory -- combinatorial number
力扣每日一题-第31天-1502.判断能否形成等差数列
Kickback -- find the first palindrome character in a group of characters
Cadence OrCAD capture "network name" is the same, but it is not connected or connected incorrectly. The usage of nodeName of liberation scheme
2022 operation of refrigeration and air conditioning equipment operation of national question bank simulated examination platform
Custom events of components ②
Implementation and encapsulation of go universal dynamic retry mechanism
【微服务|openfeign】Feign的日志记录
AUTOSAR learning record (1) – ECUM_ Init
weback5基础配置详解
Introduction to kubernetes resource objects and common commands (II)