当前位置:网站首页>[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")
}
}边栏推荐
- Swift array map/flatmap/compactmap/filter/reduce/chaining Usage Summary
- 主链系统发展解析
- test
- ## Kubernetes集群中流量暴露的几种方案 Kubernetes集群中流量暴露的几种方案
- Learn typescript with VAM (phase 1)
- Construction scheme of campus network clock system (standardized examination room)
- Elastic searchable snapshot function (frozen Tier 3)
- New MySQL 8.0 feature - enhanced logical backup recovery
- Audio knowledge (I)
- 04. Tencent cloud IOT device side learning - network connection and device authentication
猜你喜欢

A survey on dynamic neural networks for natural language processing, University of California

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

MySQL learning -- table structure of SQL test questions

A survey on model compression for natural language processing (NLP model compression overview)
![[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)

Daily algorithm & interview questions, 28 days of special training in large factories - the 15th day (string)
随机推荐
Best practices for H5 page adaptation and wechat default font size
Hook graphics kernel subsystem
Bypass kernel function pointer integrity check
A solution for building live video based on open source real-time audio and video webrtc architecture
What is thermal data detection?
Issue 003 how to detect whether a sticky positioned element is in a pinned state
Complete the log service CLS questionnaire in 1 minute and receive the Tencent cloud 30 yuan threshold free voucher ~
[leetcode108] convert an ordered array into a binary search tree (medium order traversal)
The problem is as big as the middle stage
API documents are simple and beautiful. It only needs three steps to open
zblog系统如何根据用户ID获取用户相关信息的教程
How to convert XML to HL7
Zblog system realizes the tutorial of the number of articles published on the same day when the foreground calls
重新定义存储架构,华为用了不止5颗“芯”
Recent progress of ffmpeg go
Nonholonomic constrained robot
实现TypeScript运行时类型检查
MySQL learning -- table structure of SQL test questions
构建跨公链平台解决DApp开发问题
Building a cross public chain platform to solve DAPP development problems