2022 passed by in a flash, and 2023 came quietly. One night when I read the first line of code (third edition), I saw several ways to use static methods in Kotlin. I still have some time to write a new year. Demo simple test record
在 Kotlin 中使用静态的方式不止一种,嗯... 大约有3 - 4种,具体采用哪种方式,还需要自己根据场景选择了~
Java
In 工具类
or 管理常量
, we will use static
static decoration, mainly for the调用方便
Java basic knowledge
It is mainly divided into 常规使用
and 静态使用
, which is convenient for beginners to supplement the basics ( os:现在还有新人么...
)
normal method
package com.example.ktobject;
public class JavaUtil {
public void doSomething() {
System.out.println("2023希望我们都更好,加油");
}
}
Regular call (you need to create the entity class first, and then you can call the internal method)
package com.example.ktobject
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var javaUtil = JavaUtil()
javaUtil.doSomething()
}
}
static method
package com.example.ktobject;
public class JavaUtil {
public static String anyBoy = "2023希望我们都更好,加油";
public static void doSomething() {
System.out.println("2023希望我们都更好,加油");
}
}
static call
package com.example.ktobject
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//静态变量
var anyBoy = JavaUtil.anyBoy
//静态方法
JavaUtil.doSomething()
}
}
Kotlin static variables, constants, functions
关于静态变量、常量、函数(方法)在Kotlin的使用,主要有Object、companion object、顶层方法、 @JvmStatic注解、@JvmField注解等方式
静态变量、常量
The contents of are placed in @JvmStatic、@JvmField注解
the explanation
This is a Kt utility class I created, it's just a normal class
package com.example.ktobject
class KtUtil {
fun doSomething() {
println("2023希望我们都更好,加油")
}
}
If called, you also need to call the method after creating an instance
package com.example.ktobject
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var ktUtil = KtUtil()
ktUtil.doSomething()
}
}
Singleton Object
Use Object
keywords to decorate classes
补充、扩展
- This method mainly
Kt 单例
uses the knowledge of 不支持 Java 调用 Kt 方法
- The class modified by this method can be regarded as a static class.
内部方法均为静态方法(定制性弱了一点)
- At the same
Kt
time you can also useopen
,data
,internal
package com.example.ktobject
object KtUtil {
fun doSomething() {
println("2023希望我们都更好,加油")
}
}
calling method
package com.example.ktobject
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
KtUtil.doSomething()
}
}
icon
使用不用关键字修饰类时,As 给出的图示也不相同
- class Modifies the class diagram
- Object Modified Class Diagram
companion companion object
Here using companion object
the wrapper method, only在该作用域内的方法方为静态方法
补充、扩展
- It is more customizable, and you can define the calling methods of different methods
- When calling
companion object
decoration , the principle is that a伴生类
不支持 Java 调用 Kt 方法
warts
package com.example.ktobject
class KtUtil {
fun doAnything() {
println("普通方法,不支持类名.调用")
}
companion object {
fun doSomething() {
println("2023希望我们都更好,加油")
}
}
}
calling method
package com.example.ktobject
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//基础调用(先建实例,再调方法)
KtUtil().doAnything()
//静态调用
KtUtil.doSomething()
}
}
top-level class top-level method
顶层方法是指那些没有定义在任何类中的方法,Kotlin编译器会将所有顶层方法编译成静态方法
Supplement, expand
- When this method calls a static method
无需通过 类名.方法,可直接调用方法
支持 Java 调用 Kt 方法
- I recorded an associated knowledge before . Why do some Kotlin files have a .kt suffix and some do not?
- Top-level methods are commonly used in
扩展函数、扩展属性
scenarios
Create .Kt File triple trick
- New - Kt File
- Naming-File
- initial content
KtUtils
package com.example.ktobject
fun doSomething() {
println("2023希望我们都更好,加油")
}
The call is slightly different, there is no need to pass the class name. The method can be called directly (this is the so-called top-level method)
package com.example.ktobject
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
doSomething()
}
}
icon
Annotate @JvmStatic, @JvmField (support Java tuning Kt)
我们使用 @JvmStatic 注解静态方法,使用@JvmField 注解静态参数(变量、常量)
- The original
object
method不支持Java通过静态方式调用Kt静态方法
does not need to be added to the method@JvmStatic
to support the call
package com.example.ktobject
object KtUtil {
@JvmField
var anyBoy:String ="2023希望我们都更好,加油"
@JvmStatic
fun doSomething() {
println("2023希望我们都更好,加油")
}
}
- The original
companion object
method不支持Java通过静态方式调用Kt静态方法
does not need to be added to the method@JvmStatic
to support the call
package com.example.ktobject
class KtUtil {
@JvmField
var anyBoy:String ="2023希望我们都更好,加油"
fun doAnything() {
println("普通方法,不支持类名.调用")
}
companion object {
@JvmStatic
fun doSomething() {
println("2023希望我们都更好,加油")
}
}
}
After adding corresponding comments @JvmField
,@JvmStatic
Java 可正常调 Kotlin 变量、方法等
static constant const
When using const
, you must first understand the difference between var、val
modified variables
var
mutable variable, can be changedval
Read-only variable, cannot be changed
注意: const 仅支持 val 只读变量!!!
const作用域
Generally, there are three
顶层方法
package com.example.ktobject
const val anyBoy: String = "常量(只读变量):2023希望我们都更好,加油"
object
package com.example.ktobject
object KtUtil {
const val anyBoy: String = "常量(只读变量):2023希望我们都更好,加油"
}
companion object
package com.example.ktobject
class KtUtil {
companion object{
const val anyBoy: String = "常量(只读变量):2023希望我们都更好,加油"
}
}
提示1:object、companion object 内修饰常量,可以不加const ,但是会报警告...
提示2:如果使用了const ,就不需要加 @JvmField 注解也支持Java调Kt咯
The main difference between Interest Extension → const val
and val
Decorate Object
引用性
- No const is used, in kotlin, it is directly referenced
- In java, the full path must be written and
getter
obtained through the method
Kt类
Java引用
可见性
const val
Visibility ispublic final static
, can be directly accessedval
Visibility isprivate final static
, andval
methods are generatedgetNormalObject()
, accessed through method calls