当前位置:网站首页>Kotlin companion object vs global function
Kotlin companion object vs global function
2022-07-29 03:29:00 【0 cute 0】
Kotlin The companion of (companion object) Grammatically similar to Java Static methods in , So many people use it like this . This article will show you how companion objects work and in Kotlin Recommended Practice for creating static methods in .
Companion Companion Objects
Is a class declared inside with companion Keyword marked objects . Grammatically, it is similar to Java Static methods in : You can call the members in the object through the class name .
The code is generally like this :
// Statement
class KCompanion {
companion object{
fun getBongs(nTimes: Int) {
for (i in 1 .. nTimes) {
print("BONG ")
}
}
}
}
// call
KCompanion.getBongs()It's pretty simple, isn't it ! Even if the members of the companion object look like static members in other languages , At runtime, they are still member instances of real objects .
Let's see what happens after the code is compiled :
public final class KCompanion {
...
public static final class Companion {
public final void getBongs(int nTimes) {
...
}
}
} An extra Companion class It's created . How to avoid creating this class ? We can use global functions to realize our requirements .
Global function
Create a kotlin file Bong.kt, Then write the methods we want to be accessible everywhere as static projects :
fun getBongs(nTimes: Int) {
for (i in 1 .. nTimes) {
print("BONG ")
}
}
fun someOtherMethod() {
print("BONG ")
}In this way, you can directly access the package visible functions , No qualifiers are required :
// Call global functions
getBongs(5)Let's see what the compiled code looks like :
public final class BongKt {
public static final void getBongs(int nTimes) {
...
}
public static final void someOtherMethod() {
...
}
}You can see , The compiled code does not create additional classes .
summary : If you can , When you want to use companion objects companion object when , Consider using global functions instead ~
边栏推荐
- AI platform, AI midrange architecture
- Naive Bayes -- continuous data
- Idea configuration web container and war packaging
- Anti vulnerability · benefit from uncertainty --- management?
- Server operation management system
- Machine learning [numpy]
- Principle knowledge is useful
- ShardingSphere之水平分表实战(三)
- 1.5 nn. Module neural network (III)
- 军品三大基线(功能基线、分配基线、产品基线)及基线包含的文件
猜你喜欢

VISO fast rendering convolution block

Violence recursion to dynamic programming 01 (robot movement)

mysql的timestamp存在的时区问题怎么解决

Example analysis of while, repeat and loop loops in MySQL process control

The Federal Reserve raised interest rates again, Powell "let go of doves" at 75 basis points, and US stocks reveled

Configure vscade to realize ROS writing

今晚7:30 | 连界、将门、百度、碧桂园创投四位大佬眼中的AI世界,是继续高深还是回归商业本质?...
![MOS管 —— 快速复苏应用笔记(贰)[参数与应用]](/img/54/eb040a51304192def8cfb360c7c213.png)
MOS管 —— 快速复苏应用笔记(贰)[参数与应用]

深入C语言(3)—— C的输入输出流

Rdkit II: use rdkit screening to screen 2D pharmacophores of chemical small molecules
随机推荐
STC MCU drive 1.8 'TFT SPI screen demonstration example (including data package)
MySQL流程控制之while、repeat、loop循环实例分析
Practical guidance for interface automation testing (Part I): what preparations should be made for interface automation
KNN method predicts pregnancy, KNN principle simple code
2 neural network toolbox NN
Naive Bayes -- continuous data
Incremental real-time disaster recovery notes
Introduction and advanced level of MySQL (12)
Violence recursion to dynamic programming 01 (robot movement)
(2022杭电多校三)1002-Boss Rush(状压DP+二分)
Does domestic ERP have a chance to beat sap?
Microcomputer principle operation
(codeforce547)C-Mike and Foam(质因子+容斥原理)
July 28, 2022 Gu Yujia's study notes
(nowcoder22529C)dinner(容斥原理+排列组合)
Learn exkmp again (exkmp template)
Anaconda offline installation environment
Introduction and advanced MySQL (13)
最新二开版漫画小说听书三合一完整源码/整合免签接口/搭建教程/带采集接口
Anti vulnerability · benefit from uncertainty --- management?