建议如何在Kotlin中创建常量?命名规则是什么?我在文档里没有找到。

companion object {
    //1
    val MY_CONST = "something"

    //2
    const val MY_CONST = "something"

    //3
    val myConst = "something"
}

或者…?


当前回答

我认为这将是把一个包的所有常量放在同一个文件中的最好方法,正如在其他答案中提到的,这避免了创建伴侣对象,这使得这个性能和非常类似于Java constants类。

class Constants {
    object Analytics {
        const val PROJECT_OPEN = "project_open"
        const val PROJECT_CLOSE = "project_close"
    }

    object HTTP {
        const val BASE_URL = "x.y.com"
    }

    object DBConst {
        const val TABLE_NAME = "abc"
    }
}

这可以从这样的代码中引用,使其非常结构化。

Constants.Analytics.PROJECT_OPEN
Constants.HTTP.BASE_URL
Constants.DBConst.TABLE_NAME

其他回答

在Kotlin中,如果你想创建在类中使用的局部常数,那么你可以像下面这样创建它

val MY_CONSTANT = "Constants"

如果你想在kotlin中创建一个公共常量,就像在java中创建public static final一样,你可以像下面这样创建它。

companion object{

     const val MY_CONSTANT = "Constants"

}

Something that isn't mentioned in any of the answers is the overhead of using companion objects. As you can read here, companion objects are in fact objects and creating them consumes resources. In addition, you may need to go through more than one getter function every time you use your constant. If all that you need is a few primitive constants on a few instances of your class, you'll probably just be better off using val to get a better performance and avoid the companion object. The trade off is higher memory consumption if you have many instances of your class so everyone should make their own decision.

TL,博士;文章:

使用伴侣对象实际上会将以下Kotlin代码:

class MyClass {

    companion object {
        private val TAG = "TAG"
    }

    fun helloWorld() {
        println(TAG)
    }
}

在这段Java代码中:

public final class MyClass {
    private static final String TAG = "TAG";
    public static final Companion companion = new Companion();

    // synthetic
    public static final String access$getTAG$cp() {
        return TAG;
    }

    public static final class Companion {
        private final String getTAG() {
            return MyClass.access$getTAG$cp();
        }

        // synthetic
        public static final String access$getTAG$p(Companion c) {
            return c.getTAG();
        }
    }

    public final void helloWorld() {
        System.out.println(Companion.access$getTAG$p(companion));
    }
}

如果你把const val valName = valValue放在类名之前,这样就会创建一个

public static final YourClass。Kt会有公共的静态最终值。

科特林:

const val MY_CONST0 = 0
const val MY_CONST1 = 1
data class MyClass(var some: String)

Java反编译:

public final class MyClassKt {
    public static final int MY_CONST0 = 0;
    public static final int MY_CONST1 = 1;
}
// rest of MyClass.java

我认为这将是把一个包的所有常量放在同一个文件中的最好方法,正如在其他答案中提到的,这避免了创建伴侣对象,这使得这个性能和非常类似于Java constants类。

class Constants {
    object Analytics {
        const val PROJECT_OPEN = "project_open"
        const val PROJECT_CLOSE = "project_close"
    }

    object HTTP {
        const val BASE_URL = "x.y.com"
    }

    object DBConst {
        const val TABLE_NAME = "abc"
    }
}

这可以从这样的代码中引用,使其非常结构化。

Constants.Analytics.PROJECT_OPEN
Constants.HTTP.BASE_URL
Constants.DBConst.TABLE_NAME

避免使用伴随对象。在底层,为可访问的字段创建了getter和setter实例方法。从技术上讲,调用实例方法比调用静态方法代价更大。

public class DbConstants {
    companion object {
        val TABLE_USER_ATTRIBUTE_EMPID = "_id"
        val TABLE_USER_ATTRIBUTE_DATA = "data"
    }

而是在object中定义常量。

推荐的做法:

object DbConstants {
    const val TABLE_USER_ATTRIBUTE_EMPID = "_id"
    const val TABLE_USER_ATTRIBUTE_DATA = "data"
}

并像这样全局访问它们: DbConstants。TABLE_USER_ATTRIBUTE_EMPID