您好,登錄后才能下訂單哦!
這篇文章主要介紹“java枚舉的原理是什么”,在日常操作中,相信很多人在java枚舉的原理是什么問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”java枚舉的原理是什么”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
1、枚舉是繼承了抽象類Enum的類。
Season extends java.lang.Enum<Season>
2、通過一段靜態代碼塊初始化枚舉。
static {}; descriptor: ()V flags: ACC_STATIC Code: stack=4, locals=0, args_size=0 0: new #4 // class io/github/yehongzhi/user/redisLock/Season 3: dup 4: ldc #7 // String SPRING 6: iconst_0 7: invokespecial #8 // Method "<init>":(Ljava/lang/String;I)V 10: putstatic #9 // Field SPRING:Lio/github/yehongzhi/user/redisLock/Season; 13: new #4 // class io/github/yehongzhi/user/redisLock/Season 16: dup 17: ldc #10 // String SUMMER 19: iconst_1 20: invokespecial #8 // Method "<init>":(Ljava/lang/String;I)V 23: putstatic #11 // Field SUMMER:Lio/github/yehongzhi/user/redisLock/Season; 26: new #4 // class io/github/yehongzhi/user/redisLock/Season 29: dup 30: ldc #12 // String AUTUMN 32: iconst_2 33: invokespecial #8 // Method "<init>":(Ljava/lang/String;I)V 36: putstatic #13 // Field AUTUMN:Lio/github/yehongzhi/user/redisLock/Season; 39: new #4 // class io/github/yehongzhi/user/redisLock/Season 42: dup 43: ldc #14 // String WINTER 45: iconst_3 46: invokespecial #8 // Method "<init>":(Ljava/lang/String;I)V 49: putstatic #15 // Field WINTER:Lio/github/yehongzhi/user/redisLock/Season; 52: iconst_4 53: anewarray #4 // class io/github/yehongzhi/user/redisLock/Season 56: dup 57: iconst_0 58: getstatic #9 // Field SPRING:Lio/github/yehongzhi/user/redisLock/Season; 61: aastore 62: dup 63: iconst_1 64: getstatic #11 // Field SUMMER:Lio/github/yehongzhi/user/redisLock/Season; 67: aastore 68: dup 69: iconst_2 70: getstatic #13 // Field AUTUMN:Lio/github/yehongzhi/user/redisLock/Season; 73: aastore 74: dup 75: iconst_3 76: getstatic #15 // Field WINTER:Lio/github/yehongzhi/user/redisLock/Season; 79: aastore 80: putstatic #1 // Field $VALUES:[Lio/github/yehongzhi/user/redisLock/Season; 83: return
這段靜態代碼塊的作用就是生成四個靜態常量字段的值,還生成了$VALUES字段,用于保存枚舉類定義的枚舉常量。
3、關于values()方法,這是一個靜態方法,作用是返回該枚舉類的數組,底層實現原理,其實是這樣的。
public static io.github.yehongzhi.user.redisLock.Season[] values(); Code: 0: getstatic #1 // Field $VALUES:[Lio/github/yehongzhi/user/redisLock/Season; 3: invokevirtual #2 // Method "[Lio/github/yehongzhi/user/redisLock/Season;".clone:()Ljava/lang/Object; 6: checkcast #3 // class "[Lio/github/yehongzhi/user/redisLock/Season;" 9: areturn
到此,關于“java枚舉的原理是什么”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。