当前位置:网站首页>[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") } }
边栏推荐
- Can yangjianyun's new media operation in 2021 bear all the expectations of the enterprise's private domain traffic demand?
- zblog判断某个插件是否安装启用的内置函数代码
- [tke] enable CPU static management strategy
- As for IOT safety, 20 CSOs from major manufacturers say
- How to perform concurrent stress testing on RTSP video streams distributed by audio and video streaming servers?
- Yiwen teaches you to understand the stack operation in go
- 问题有多大,中台就有多大
- After the collective breakthrough, where is the next step of China's public cloud?
- Is CICC securities reliable? Is it legal? Is it safe to open a stock account?
- Talk about some good ways to participate in the project
猜你喜欢
MySQL learning -- table structure of SQL test questions
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"
[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)
A survey on model compression for natural language processing (NLP model compression overview)
随机推荐
API documents are simple and beautiful. It only needs three steps to open
Implement typescript runtime type checking
How to customize the log output format of zap?
Yiwen teaches you to understand the stack operation in go
Easycvr, an urban intelligent video monitoring image analysis platform, plays national standard equipment videos and captures unstable packets for troubleshooting
Building a cross public chain platform to solve DAPP development problems
Private domain defense in the cotton Era
Future banks need to think about today's structure with tomorrow's thinking
This time, talk about the dry goods of industrial Internet | TVP technology closed door meeting
Introduction to koa (IV) koa operation database
H265/webvr video web page without plug-in player easyplayer Solution to the problem of cumulative delay of FLV video played by JS
Automatically refresh CDN cache in real time based on cos log
How important is it to document the project? I was chosen by the top 100 up leaders and stood up again
After the collective breakthrough, where is the next step of China's public cloud?
Introduction to koa (II) building the koa program
[play with Tencent cloud] & lt; trtc-room> Applet component usage
Recent progress of ffmpeg go
Markdown syntax -- Formula
Can you remember the code of a programming boss? Can you hit it out without Baidu?
Bypass kernel function pointer integrity check