温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java枚举的原理是什么

发布时间:2022-06-01 15:16:17 阅读:214 作者:iii 栏目:大数据
Java开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

这篇文章主要介绍“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
        10putstatic     #9                  // Field SPRING:Lio/github/yehongzhi/user/redisLock/Season;
        13new           #4                  // class io/github/yehongzhi/user/redisLock/Season
        16dup
        17ldc           #10                 // String SUMMER
        19iconst_1
        20invokespecial #8                  // Method "<init>":(Ljava/lang/String;I)V
        23putstatic     #11                 // Field SUMMER:Lio/github/yehongzhi/user/redisLock/Season;
        26new           #4                  // class io/github/yehongzhi/user/redisLock/Season
        29dup
        30ldc           #12                 // String AUTUMN
        32iconst_2
        33invokespecial #8                  // Method "<init>":(Ljava/lang/String;I)V
        36putstatic     #13                 // Field AUTUMN:Lio/github/yehongzhi/user/redisLock/Season;
        39new           #4                  // class io/github/yehongzhi/user/redisLock/Season
        42dup
        43ldc           #14                 // String WINTER
        45iconst_3
        46invokespecial #8                  // Method "<init>":(Ljava/lang/String;I)V
        49putstatic     #15                 // Field WINTER:Lio/github/yehongzhi/user/redisLock/Season;
        52iconst_4
        53anewarray     #4                  // class io/github/yehongzhi/user/redisLock/Season
        56dup
        57iconst_0
        58getstatic     #9                  // Field SPRING:Lio/github/yehongzhi/user/redisLock/Season;
        61aastore
        62dup
        63iconst_1
        64getstatic     #11                 // Field SUMMER:Lio/github/yehongzhi/user/redisLock/Season;
        67aastore
        68dup
        69iconst_2
        70getstatic     #13                 // Field AUTUMN:Lio/github/yehongzhi/user/redisLock/Season;
        73aastore
        74dup
        75iconst_3
        76getstatic     #15                 // Field WINTER:Lio/github/yehongzhi/user/redisLock/Season;
        79aastore
        80putstatic     #1                  // Field $VALUES:[Lio/github/yehongzhi/user/redisLock/Season;
        83return

这段静态代码块的作用就是生成四个静态常量字段的值,还生成了$VALUES字段,用于保存枚举类定义的枚举常量。

3、关于values()方法,这是一个静态方法,作用是返回该枚举类的数组,底层实现原理,其实是这样的。

public static io.github.yehongzhi.user.redisLock.Season[] values();
    Code:
       0getstatic     #1                  // Field $VALUES:[Lio/github/yehongzhi/user/redisLock/Season;
       3invokevirtual #2                  // Method "[Lio/github/yehongzhi/user/redisLock/Season;".clone:()Ljava/lang/Object;
       6checkcast     #3                  // class "[Lio/github/yehongzhi/user/redisLock/Season;"
       9areturn

到此,关于“java枚举的原理是什么”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

原文链接:https://my.oschina.net/u/4600288/blog/4472386

AI

开发者交流群×