Kotlin static methods and static classes

Kotlin static methods and static classes

static class

All methods are static methods, such as tool classes, constant pools, etc., directly replace the class before the class name with object

object Constants1 {
    val text = "hello world!"
}

transfer

Constants1.text

static method

In Kotlin, static methods can be implemented as "companion objects". The specific operations are:

Inside the class wrap required static functions with companion object { }

class Constants {
    companion object {
        val text = "hello world!"
    }
}

transfer

Constants.text

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324848384&siteId=291194637