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

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

    //2
    const val MY_CONST = "something"

    //3
    val myConst = "something"
}

或者…?


当前回答

当地常量:

const val NAME = "name"

全局常量:

object MyConstants{
    val NAME = "name"
    val ID = "_id"
    var EMAIL = "email"
}

访问MyConstants。的名字

其他回答

避免使用伴随对象。在底层,为可访问的字段创建了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

像val一样,用const关键字定义的变量是不可变的。这里的区别在于,const用于编译时已知的变量。

声明一个变量const很像在Java中使用static关键字。

让我们看看如何在Kotlin中声明一个const变量:

const val COMMUNITY_NAME = "wiki"

用Java编写的类似代码是:

final static String COMMUNITY_NAME = "wiki";

加上上面的答案-

@JvmField可以用来指示Kotlin编译器不生成 getter /setter,并将其公开为字段。

 @JvmField
 val COMMUNITY_NAME = "Wiki"

静态字段

在命名对象或伴生对象中声明的Kotlin属性 将有静态支持字段,无论是在该命名对象或 包含伴生对象的类。

通常这些字段是私有的,但它们可以通过以下方式之一公开:

@JvmField注释; lateinit修饰语; const修饰符。

更多详情请访问:https://kotlinlang.org/docs/reference/java-to-kotlin-interop.html#instance-fields

在Kotlin中,当我们声明变量时,有两个选项。'var'或'val'。 遵循变量的命名惯例,我认为我们可以简单地生成“常量”,这意味着我想要一个固定的值分配给一个特定的变量,就像下面的示例代码。

private val tag = "MainActivity"

我认为我们不需要像在Java世界中所做的那样,麻烦地使用大写字母来区分“常量”和变量。

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));
    }
}

Kotlin静态和常量值&方法声明

object MyConstant {

@JvmField   // for access in java code 
val PI: Double = 3.14

@JvmStatic // JvmStatic annotation for access in java code
fun sumValue(v1: Int, v2: Int): Int {
    return v1 + v2
}

}

在任何地方访问值

val value = MyConstant.PI
val value = MyConstant.sumValue(10,5)