概述:
enum在java中的實質是特殊單例的靜態成員變量。在運行期,所有枚舉類作為單例,全部加載到內存中。
所以,枚舉增加了運行時的內存占用。
使用@IntDef/@StringDef + @interface
來進行限定參數:
@IntDef({ItemState.ADD, ItemState.SUCCESS, ItemState.LOADING, ItemState.FAIL}) @Retention(RetentionPolicy.SOURCE) public @interface ItemState { int ADD = 0; int SUCCESS = 1; int LOADING = 2; int FAIL = 3; }
RetentionPolicy有3個值:CLASS RUNTIME SOURCE
用@Retention(RetentionPolicy.CLASS)修飾的注解,表示注解的信息被保留在class文件(字節碼文件)中當程序編譯時,但不會被虛擬機讀取在運行的時候;
用@Retention(RetentionPolicy.SOURCE )修飾的注解,表示注解的信息會被編譯器拋棄,不會留在class文件中,注解的信息只會留在源文件中;
用@Retention(RetentionPolicy.RUNTIME )修飾的注解,表示注解的信息被保留在class文件(字節碼文件)中當程序編譯時,會被虛擬機保留在運行時,
private @ItemState int getState(int position) { final int actualItemCount = getActualItemCount(); if ((actualItemCount < mMaxCount && (mLoadingViewIndex + mLoadingViewCount) < mMaxCount) && position == getItemCount() - 1) { return ItemState.ADD; } else if (isShowLoading(position)) { return ItemState.LOADING; } else if (position >= 0 && position < actualItemCount && FAIL_VIEW_PLACE_TEXT.equalsIgnoreCase(getItem(position))){ return ItemState.FAIL; } else { return ItemState.SUCCESS; } }
public interface ILoginMethod { @IntDef({Method.OTHER, Method.ACCOUNT, Method.THIRD, Method.TOKEN, Method.MOBILE_CODE, Method.MOBILE_CMCC, Method.KG_FAST, Method.REGISTER, Method.CERTIFYCODE, Method.SMS}) @Retention(RetentionPolicy.SOURCE) @interface Method { int OTHER = 0; int THIRD = 1; int ACCOUNT = 2; int TOKEN = 3; int MOBILE_CODE = 4; int KG_FAST = 5; int REGISTER = 6; int CERTIFYCODE = 7; // 掃臉認證成功后,通過token登錄 int MOBILE_CMCC = 8; // 移動一鍵登錄 int SMS = 9; // 短信驗證碼登錄 } void login(); @Method int getMethod(); }