您好,登錄后才能下訂單哦!
java中枚舉的原理是什么?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
Java主要應(yīng)用于:1. web開發(fā);2. Android開發(fā);3. 客戶端開發(fā);4. 網(wǎng)頁(yè)開發(fā);5. 企業(yè)級(jí)應(yīng)用開發(fā);6. Java大數(shù)據(jù)開發(fā);7.游戲開發(fā)等。
1、枚舉是繼承了抽象類Enum的類。
Season extends java.lang.Enum<Season>
2、通過一段靜態(tài)代碼塊初始化枚舉。
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
這段靜態(tài)代碼塊的作用就是生成四個(gè)靜態(tài)常量字段的值,還生成了$VALUES字段,用于保存枚舉類定義的枚舉常量。
3、關(guān)于values()方法,這是一個(gè)靜態(tài)方法,作用是返回該枚舉類的數(shù)組,底層實(shí)現(xiàn)原理,其實(shí)是這樣的。
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
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。