“枚举”的用法是什么在Java中? [英] What's the use of "enum" in Java?

查看:85
本文介绍了“枚举”的用法是什么在Java中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我调查了这个枚举类型,它似乎是一个荣耀的数组 / ArrayList / 列表给我。究竟是用什么呢?

解决方案

枚举是一种固定数量的常量,至少可以用于两件事情。
$ b

常量

  public enum Month {
JANUARY ,FEBRUARY,...
}

这比创建一堆整数好多了常数。



创建单身人士

  public enum Singleton {
INSTANCE

// init
};

你可以用枚举来做相当有趣的事情,看看这里



还要看看官方文件


So I looked into this "enum" type, and it kind of seems like a glorified array/ArrayList/List to me. What exactly is the use of it?

解决方案

Enum serves as a type of fixed number of constants and can be used at least for two things

constant

public enum Month {
    JANUARY, FEBRUARY, ...
}

This is much better than creating a bunch of integer constants.

creating a singleton

public enum Singleton {
    INSTANCE

   // init
};

You can do quite interesting things with enums, look at here

Also look at the official documentation

这篇关于“枚举”的用法是什么在Java中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆