搜尋
首頁Javajava教程Java枚舉的原理是什麼?

Java枚舉的原理是什麼?

Apr 20, 2023 pm 10:37 PM
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枚舉的原理是什麼?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:亿速云。如有侵權,請聯絡admin@php.cn刪除
JWT能否實現動態權限變更?與Session機制有何區別?JWT能否實現動態權限變更?與Session機制有何區別?Apr 19, 2025 pm 06:12 PM

關於JWT和Session的困惑與解答許多初學者在學習JWT和Session時,常常會對其本質和適用場景感到困惑。本文將圍繞J...

Windows Server 2019防火牆如何正確配置才能支持WebSocket通信?Windows Server 2019防火牆如何正確配置才能支持WebSocket通信?Apr 19, 2025 pm 06:09 PM

WindowsServer2019防火牆與WebSocket通信問題詳解在使用SpringBoot開發的Jar程序部署於WindowsServer2019...

Spring Boot子線程如何訪問主線程的請求信息?Spring Boot子線程如何訪問主線程的請求信息?Apr 19, 2025 pm 06:03 PM

SpringBoot子線程無法訪問主線程Request信息解決方案在Spring...

Java單線程下的指令重排序會影響System.out.println的輸出順序嗎?Java單線程下的指令重排序會影響System.out.println的輸出順序嗎?Apr 19, 2025 pm 06:00 PM

Java單線程下的指令重排序與輸出順序在Java編程中,指令重排序是一個常見的優化技術,用於提高程序的執行效�...

IntelliJ IDEA是如何通過JavaAgent技術識別Spring Boot項目的端口號的?IntelliJ IDEA是如何通過JavaAgent技術識別Spring Boot項目的端口號的?Apr 19, 2025 pm 05:57 PM

IntelliJIDEA如何識別SpringBoot項目的端口號?在使用IntelliJIDEAUltimate版本時,啟動Spring...

如何通過 OAuth2.0 的 scope 機制精細控制 access_token 的接口訪問權限?如何通過 OAuth2.0 的 scope 機制精細控制 access_token 的接口訪問權限?Apr 19, 2025 pm 05:54 PM

通過OAuth2.0的access_token如何精細控制接口訪問權限?在現代應用開發中,OAuth2.0...

RuoYi框架如何實現Bean依賴注入而無需顯式編寫DataSource實現類?RuoYi框架如何實現Bean依賴注入而無需顯式編寫DataSource實現類?Apr 19, 2025 pm 05:51 PM

深入剖析RuoYi框架的Bean依賴注入機制:無需顯式實現類RuoYi框架是一個流行的Java前後端分離框架,其簡潔的代碼...

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱工具

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用