日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java在枚举方法中调方法_java – 值方法如何在枚举中工作

發布時間:2025/3/15 编程问答 15 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java在枚举方法中调方法_java – 值方法如何在枚举中工作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在Enum中value()方法如何工作?

values()方法背后的邏輯是什么?

在我的項目中,我們將所有枚舉數據緩存在Map中,如下所示:

public enum Actions {

CREATE("create"),

UPDATE("update"),

DELETE("delete"),

ACTIVE("active"),

INACTIVE("inactive"),

MANAGE_ORDER("manage"),

;

private static Map actionMap;

static {

actionMap = new HashMap(values().length);

for(Actions action : values()) {

actionMap.put(action.getName(), action);

}

}

public static Actions fromName(String name) {

if(name == null)

throw new IllegalArgumentException();

return actionMap.get(name);

}

private String name;

private Actions(String name) {

this.name = name;

}

public String getName() {

return name;

}

}

這是使用枚舉的最佳做法嗎?

解決方法:

In Enum how values() method work ?

The enum declaration defines a class (called an enum type). The enum class body can include methods and other fields. The compiler automatically adds some special methods when it creates an enum. For example, they have a static values method that returns an array containing all of the values of the enum in the order they are declared. This method is commonly used in combination with the for-each construct to iterate over the values of an enum type.

另外需要注意的是,如果你使用枚舉作為鍵,最好使用EnumMap.

A specialized Map implementation for use with enum type keys. All of the keys in an enum map must come from a single enum type that is specified, explicitly or implicitly, when the map is created. Enum maps are represented internally as arrays. This representation is extremely compact and efficient.

標簽:java

來源: https://codeday.me/bug/20190723/1509014.html

總結

以上是生活随笔為你收集整理的java在枚举方法中调方法_java – 值方法如何在枚举中工作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。