当前位置:网站首页>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
边栏推荐
- MATLAB之基础知识
- 力扣每日一题-第31天-1502.判断能否形成等差数列
- 良心安利万向轮 SolidWorks模型素材网站
- Redisson utilise la solution complète - redisson Documents officiels + commentaires (Partie 1)
- Browser local storage
- [skill] create Bat quick open web page
- Jax's deep learning and scientific computing
- Thesis learning -- Analysis and Research on similarity query of hydrological time series
- She is the "HR of others" | ones character
- The triode is a great invention
猜你喜欢

How to use layui to display the data in the database in the form of tables

How outlook puts together messages with the same discussion

The database is locked. Is there a solution

下载Xshell和Xftp

Conscience Amway universal wheel SolidWorks model material website

redisson使用全解——redisson官方文档+注释(中篇)

kubernetes资源对象介绍及常用命令(二)

Jax's deep learning and scientific computing

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

2022 mobile crane driver test exercises and online simulation test
随机推荐
[MySQL learning notes27] stored procedure
Software testing methods and techniques - overview of basic knowledge
2022 Guangdong Provincial Safety Officer a certificate third batch (main person in charge) special operation certificate examination question bank simulated examination platform operation
力扣每日一题-第31天-1502.判断能否形成等差数列
Li Kou daily question - day 31 -1790 Can a string exchange be performed only once to make two strings equal
PWN攻防世界int_overflow
Array: question brushing record
组件的自定义事件②
038 network security JS
2022 mobile crane driver test exercises and online simulation test
How relational databases work
Thesis learning -- Analysis and Research on similarity query of hydrological time series
Is it safe and reliable for Huatai Securities to open an account? How to open Huatai Securities Account
Vhost kick & call principle
【无标题】
Implementation and encapsulation of go universal dynamic retry mechanism
redisson使用全解——redisson官方文檔+注釋(上篇)
浏览器本地存储
Redisson uses the full solution - redisson official documents + comments (Part 2)
良心安利万向轮 SolidWorks模型素材网站