kotlin-变量和常量

1.变量

kotlin 支持类型推断

//可变变量
var str = "Android"
//不可变变量,相当于java中用final修饰的变量
val str = "Android"

2.常量

//1.声明在顶层
const val ANDROID = "Android"
class xxx{
}
//2.object
object xxx{
   const val ANDROID = "Android"
}
//3.伴生对象
class xxx{
    companion object{
        const val ANDROID = "Android"
    }
}

3.伴生对象和顶层成员的实现

const val CONST_OBJECT = 0

class TopLevelFunction {

    companion object {
        const val CONST_OBJECT = 0
    }
}

转成 java

// TopLevelFunction.java
package com.example.firstdemo;

import kotlin.Metadata;
import kotlin.jvm.internal.DefaultConstructorMarker;
import org.jetbrains.annotations.NotNull;

@Metadata(
   mv = {1, 8, 0},
   k = 1,
   d1 = {"\u0000\f\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0003\u0018\u0000 \u00032\u00020\u0001:\u0001\u0003B\u0005¢\u0006\u0002\u0010\u0002¨\u0006\u0004"},
   d2 = {"Lcom/example/firstdemo/TopLevelFunction;", "", "()V", "Companion", "app_debug"}
)
public final class TopLevelFunction {
   public static final int CONST_OBJECT = 0;
   @NotNull
   public static final Companion Companion = new Companion((DefaultConstructorMarker)null);

   @Metadata(
      mv = {1, 8, 0},
      k = 1,
      d1 = {"\u0000\u0012\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0002\n\u0002\u0010\b\n\u0000\b\u0086\u0003\u0018\u00002\u00020\u0001B\u0007\b\u0002¢\u0006\u0002\u0010\u0002R\u000e\u0010\u0003\u001a\u00020\u0004X\u0086T¢\u0006\u0002\n\u0000¨\u0006\u0005"},
      d2 = {"Lcom/example/firstdemo/TopLevelFunction$Companion;", "", "()V", "CONST_OBJECT", "", "app_debug"}
   )
   public static final class Companion {
      private Companion() {
      }

      // $FF: synthetic method
      public Companion(DefaultConstructorMarker $constructor_marker) {
         this();
      }
   }
}
// TopLevelFunctionKt.java
package com.example.firstdemo;

import kotlin.Metadata;

@Metadata(
   mv = {1, 8, 0},
   k = 2,
   d1 = {"\u0000\b\n\u0000\n\u0002\u0010\b\n\u0000\"\u000e\u0010\u0000\u001a\u00020\u0001X\u0086T¢\u0006\u0002\n\u0000¨\u0006\u0002"},
   d2 = {"CONST_OBJECT", "", "app_debug"}
)
public final class TopLevelFunctionKt {
   public static final int CONST_OBJECT = 0;
}

伴生对象是利用内部类实现的
顶层成员是重新声明了一个类
(我在2个不同的文件,声明同一个顶层成员,发现不可以)

调用方式

 //顶层函数
 CONST_OBJECT
 //伴生对象
 TopLevelFunction.CONST_OBJECT

4.object

object ConstObject {
    const val CONST_OBJECT = 0
}
package com.example.firstdemo;

import kotlin.Metadata;
import org.jetbrains.annotations.NotNull;

@Metadata(
   mv = {1, 8, 0},
   k = 1,
   d1 = {"\u0000\u0012\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0002\n\u0002\u0010\b\n\u0000\bÆ\u0002\u0018\u00002\u00020\u0001B\u0007\b\u0002¢\u0006\u0002\u0010\u0002R\u000e\u0010\u0003\u001a\u00020\u0004X\u0086T¢\u0006\u0002\n\u0000¨\u0006\u0005"},
   d2 = {"Lcom/example/firstdemo/ConstObject;", "", "()V", "CONST_OBJECT", "", "app_debug"}
)
public final class ConstObject {
   public static final int CONST_OBJECT = 0;
   @NotNull
   public static final ConstObject INSTANCE;

   private ConstObject() {
   }

   static {
      ConstObject var0 = new ConstObject();
      INSTANCE = var0;
   }
}

调用

ConstObject.CONST_OBJECT

总结

伴生对象的方式适合类内部使用的常量 (类内部的私有常量)
顶层函数和object声明的常量适合多个类之间的调用 (Intent传递的Key)

你可能感兴趣的:(kotlin,kotlin,android,android,studio)