在Kotlin中創(chuàng)建一個(gè)屬性棠涮,如果是val
,那么會自動生成getter
方法蟆湖,如果是var
故爵,那么會自動生成getter
和setter
方法。
class MethodFoo() {
var a: Int = 1
var b: Int = 2
}
生成的class內(nèi)容
public final class MethodFoo {
private int a = 1;
private int b = 2;
public final int getA() {
return this.a;
}
public final void setA(int var1) {
this.a = var1;
}
public final int getB() {
return this.b;
}
public final void setB(int var1) {
this.b = var1;
}
}
自動生成get
和set
在開發(fā)的時(shí)候隅津,經(jīng)常會用到一些常量诬垂,而常量一般放在一個(gè)公共的類里面,在Kotlin中伦仍,我們是這樣寫的:
object Constant {
val ACTION_FINDVIEWBYID = "FindViewById"
val ACTION_BUTTERKNIFE = "ButterKnife"
val SELECTED_TEXT_SUFFIX = ".xml"
val PSI_METHOD_BY_ONCREATE = "onCreate"
val PSI_METHOD_BY_ONCREATEVIEW = "onCreateView"
val CREATOR_COMMAND_NAME = "Generate Injections"
val CLASS_TYPE_BY_ACTIVITY = "activity"
val CLASS_TYPE_BY_FRAGMENT = "fragment"
val FIELD_ON_CLICK = "OnClick"
val FIELD_ONCLICK = "onClick"
}
生成的代碼是這樣的:
public final class Constant {
@NotNull
private static final String ACTION_FINDVIEWBYID = "FindViewById";
@NotNull
private static final String ACTION_BUTTERKNIFE = "ButterKnife";
@NotNull
private static final String SELECTED_TEXT_SUFFIX = ".xml";
@NotNull
private static final String PSI_METHOD_BY_ONCREATE = "onCreate";
@NotNull
private static final String PSI_METHOD_BY_ONCREATEVIEW = "onCreateView";
@NotNull
private static final String CREATOR_COMMAND_NAME = "Generate Injections";
@NotNull
private static final String CLASS_TYPE_BY_ACTIVITY = "activity";
@NotNull
private static final String CLASS_TYPE_BY_FRAGMENT = "fragment";
@NotNull
private static final String FIELD_ON_CLICK = "OnClick";
@NotNull
private static final String FIELD_ONCLICK = "onClick";
public static final Constant INSTANCE;
@NotNull
public final String getACTION_FINDVIEWBYID() {
return ACTION_FINDVIEWBYID;
}
@NotNull
public final String getACTION_BUTTERKNIFE() {
return ACTION_BUTTERKNIFE;
}
@NotNull
public final String getSELECTED_TEXT_SUFFIX() {
return SELECTED_TEXT_SUFFIX;
}
@NotNull
public final String getPSI_METHOD_BY_ONCREATE() {
return PSI_METHOD_BY_ONCREATE;
}
@NotNull
public final String getPSI_METHOD_BY_ONCREATEVIEW() {
return PSI_METHOD_BY_ONCREATEVIEW;
}
@NotNull
public final String getCREATOR_COMMAND_NAME() {
return CREATOR_COMMAND_NAME;
}
@NotNull
public final String getCLASS_TYPE_BY_ACTIVITY() {
return CLASS_TYPE_BY_ACTIVITY;
}
@NotNull
public final String getCLASS_TYPE_BY_FRAGMENT() {
return CLASS_TYPE_BY_FRAGMENT;
}
@NotNull
public final String getFIELD_ON_CLICK() {
return FIELD_ON_CLICK;
}
@NotNull
public final String getFIELD_ONCLICK() {
return FIELD_ONCLICK;
}
private Constant() {
INSTANCE = (Constant)this;
ACTION_FINDVIEWBYID = "FindViewById";
ACTION_BUTTERKNIFE = "ButterKnife";
SELECTED_TEXT_SUFFIX = ".xml";
PSI_METHOD_BY_ONCREATE = "onCreate";
PSI_METHOD_BY_ONCREATEVIEW = "onCreateView";
CREATOR_COMMAND_NAME = "Generate Injections";
CLASS_TYPE_BY_ACTIVITY = "activity";
CLASS_TYPE_BY_FRAGMENT = "fragment";
FIELD_ON_CLICK = "OnClick";
FIELD_ONCLICK = "onClick";
}
static {
new Constant();
}
}
這....生成了那么多的get
方法结窘,如果我有100個(gè)字段,那么就多生成了100個(gè)get
方法充蓝,我只是想要Constant.fieldName
這樣子調(diào)用這個(gè)字段而已隧枫,根本不需要用到get
,這樣明顯是創(chuàng)建了多余的方法谓苟,方法數(shù)增多容易報(bào)65535
錯(cuò)官脓。
如何避免生成
在Kotlin中,可以使用兩種方法來停止自動生成get
和set
方法涝焙。
使用const
關(guān)鍵字
object Constant {
const val ACTION_FINDVIEWBYID = "FindViewById"
const val ACTION_BUTTERKNIFE = "ButterKnife"
const val SELECTED_TEXT_SUFFIX = ".xml"
const val PSI_METHOD_BY_ONCREATE = "onCreate"
const val PSI_METHOD_BY_ONCREATEVIEW = "onCreateView"
const val CREATOR_COMMAND_NAME = "Generate Injections"
const val CLASS_TYPE_BY_ACTIVITY = "activity"
const val CLASS_TYPE_BY_FRAGMENT = "fragment"
const val FIELD_ON_CLICK = "OnClick"
const val FIELD_ONCLICK = "onClick"
}
來看一下編譯后的內(nèi)容
public final class Constant {
@NotNull
public static final String ACTION_FINDVIEWBYID = "FindViewById";
@NotNull
public static final String ACTION_BUTTERKNIFE = "ButterKnife";
@NotNull
public static final String SELECTED_TEXT_SUFFIX = ".xml";
@NotNull
public static final String PSI_METHOD_BY_ONCREATE = "onCreate";
@NotNull
public static final String PSI_METHOD_BY_ONCREATEVIEW = "onCreateView";
@NotNull
public static final String CREATOR_COMMAND_NAME = "Generate Injections";
@NotNull
public static final String CLASS_TYPE_BY_ACTIVITY = "activity";
@NotNull
public static final String CLASS_TYPE_BY_FRAGMENT = "fragment";
@NotNull
public static final String FIELD_ON_CLICK = "OnClick";
@NotNull
public static final String FIELD_ONCLICK = "onClick";
public static final Constant INSTANCE;
private Constant() {
INSTANCE = (Constant)this;
}
static {
new Constant();
}
}
使用@JvmField
注解
object Constant {
@JvmField val ACTION_FINDVIEWBYID = "FindViewById"
@JvmField val ACTION_BUTTERKNIFE = "ButterKnife"
@JvmField val SELECTED_TEXT_SUFFIX = ".xml"
@JvmField val PSI_METHOD_BY_ONCREATE = "onCreate"
@JvmField val PSI_METHOD_BY_ONCREATEVIEW = "onCreateView"
@JvmField val CREATOR_COMMAND_NAME = "Generate Injections"
@JvmField val CLASS_TYPE_BY_ACTIVITY = "activity"
@JvmField val CLASS_TYPE_BY_FRAGMENT = "fragment"
@JvmField val FIELD_ON_CLICK = "OnClick"
@JvmField val FIELD_ONCLICK = "onClick"
}
來看一下編譯后的內(nèi)容
public final class Constant {
@JvmField
@NotNull
public static final String ACTION_FINDVIEWBYID = "FindViewById";
@JvmField
@NotNull
public static final String ACTION_BUTTERKNIFE = "ButterKnife";
@JvmField
@NotNull
public static final String SELECTED_TEXT_SUFFIX = ".xml";
@JvmField
@NotNull
public static final String PSI_METHOD_BY_ONCREATE = "onCreate";
@JvmField
@NotNull
public static final String PSI_METHOD_BY_ONCREATEVIEW = "onCreateView";
@JvmField
@NotNull
public static final String CREATOR_COMMAND_NAME = "Generate Injections";
@JvmField
@NotNull
public static final String CLASS_TYPE_BY_ACTIVITY = "activity";
@JvmField
@NotNull
public static final String CLASS_TYPE_BY_FRAGMENT = "fragment";
@JvmField
@NotNull
public static final String FIELD_ON_CLICK = "OnClick";
@JvmField
@NotNull
public static final String FIELD_ONCLICK = "onClick";
public static final Constant INSTANCE;
private Constant() {
INSTANCE = (Constant)this;
ACTION_FINDVIEWBYID = "FindViewById";
ACTION_BUTTERKNIFE = "ButterKnife";
SELECTED_TEXT_SUFFIX = ".xml";
PSI_METHOD_BY_ONCREATE = "onCreate";
PSI_METHOD_BY_ONCREATEVIEW = "onCreateView";
CREATOR_COMMAND_NAME = "Generate Injections";
CLASS_TYPE_BY_ACTIVITY = "activity";
CLASS_TYPE_BY_FRAGMENT = "fragment";
FIELD_ON_CLICK = "OnClick";
FIELD_ONCLICK = "onClick";
}
static {
new Constant();
}
}
后記
當(dāng)我們使用Kotlin定義大量的公共字段的時(shí)候卑笨,一定要這樣寫來避免方法數(shù)過多。