“枚举"有什么用?在爪哇? [英] What's the use of "enum" in Java?

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

问题描述

所以我研究了这个枚举"类型,对我来说它有点像一个美化的array/ArrayList/List.它的具体用途是什么?

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 作为一种固定数量的常量,至少可以用于两件事

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

恒定

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

这比创建一堆整数常量要好得多.

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

创建单例

public enum Singleton {
    INSTANCE

   // init
};

你可以用枚举做一些非常有趣的事情,看看这里

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

另看官方文档

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

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