当前位置:网站首页>[kotlin] constructor summary
[kotlin] constructor summary
2022-06-24 17:14:00 【User 3702315】
Primary constructor
// Kotlin The constructor of can be written in the class header , After the class name .
// The constructor declared in this way , We call it the main constructor .
class Person(private val name: String) {
fun sayHello() {
// Arguments declared in the main constructor , They are public fields of the class by default .
println("hello $name")
}
}
// Consistent with the above functions : Declare primary constructor
class Person constructor(private val name: String) {
fun sayHello() {
println("hello $name")
}
}
/// Consistent with the above functions , The modifier defaults to :public
class Person public constructor(private val name: String) {
fun sayHello() {
println("hello $name")
}
}
// If the annotation is on the main constructor , Keywords are required :constructor
class Person @TargetApi(28) constructor(private val name: String) {
fun sayHello() {
println("hello $name")
}
}
// If there is extra code that needs to be executed in the constructor , Need to put init Code block
class Person(private var name: String) {
init {
name = "Hello World"
}
internal fun sayHello() {
println("hello $name")
}
}Secondary constructor
class Person(private var name: String) {
private var description: String? = null
init {
name = "Hello World"
}
// If there is a primary constructor , Inherit the main constructor
constructor(name: String, description: String) : this(name) {
this.description = description
}
internal fun sayHello() {
println("hello $name")
}
}边栏推荐
- Saying "Dharma" Today: the little "secret" of paramter and localparam
- Cloud native monitoring via blackbox_ Exporter monitoring website
- 跟着Vam一起学习Typescript(第一期)
- Yuanqi forest started from 0 sugar and fell at 0 sugar
- Solution to the problem that kibana's map cannot render longitude and latitude coordinate data
- 究竟有哪些劵商推荐?现在网上开户安全么?
- Introduction to koa (IV) koa operation database
- 区块哈希游戏竞猜系统开发(成熟代码)
- Snapshot management for elastic cloud enterprise
- Game business DDoS attack and defense confrontation case sharing
猜你喜欢

Why do you develop middleware when you are young? "You can choose your own way"

Daily algorithm & interview questions, 28 days of special training in large factories - the 15th day (string)

A survey on dynamic neural networks for natural language processing, University of California
![[leetcode108] convert an ordered array into a binary search tree (medium order traversal)](/img/e1/0fac59a531040d74fd7531e2840eb5.jpg)
[leetcode108] convert an ordered array into a binary search tree (medium order traversal)

MySQL learning -- table structure of SQL test questions

A survey on model compression for natural language processing (NLP model compression overview)
随机推荐
Hook graphics kernel subsystem
Pagoda activities, team members can enjoy a lightweight server 1 core 2g5m 28 yuan for two years
A survey on model compression for natural language processing (NLP model compression overview)
Ramda's little-known side
Game business DDoS attack and defense confrontation case sharing
The problem is as big as the middle stage
A solution for building live video based on open source real-time audio and video webrtc architecture
Prometheus deployment
Markdown syntax -- Formula
API documents are simple and beautiful. It only needs three steps to open
Future banks need to think about today's structure with tomorrow's thinking
FPGA systematic learning notes serialization_ Day8 [design of 4-bit multiplier and 4-bit divider]
Tiktok Kwai, e-commerce enters the same river
FPGA project development: experience sharing of lmk04821 chip project development based on jesd204b (I)
Construction scheme of campus network clock system (standardized examination room)
[tke] nodelocaldnschache is used in IPVS forwarding mode
Low education without food? As an old Android rookie in the past six years, I was the most difficult one
Go path customized project path package dependency
Collect tke logs through daemonset CRD
06. Tencent cloud IOT device side learning - Introduction to basic functions