一.枚舉和靜態(tài)常量區(qū)別
講到枚舉我們首先思考,它和public static final string 修飾的常量有什么不同。
我舉枚舉的兩個(gè)優(yōu)點(diǎn):
1. 保證了類型安全:調(diào)用者無法隨意傳一個(gè) int或者string 等值;
2.代碼可讀性非常高;
舉個(gè)例子:
在實(shí)際編程中,往往存在著這樣的“數(shù)據(jù)集”,它們的數(shù)值在程序中是穩(wěn)定的,而且“數(shù)據(jù)集”中的元素是有限的。例如春夏秋冬四個(gè)數(shù)據(jù)元素組成了四季的“數(shù)據(jù)集”。
你寫了方法get(string season),輸入的類型只能是string類型,同時(shí)要string只能是(春、夏。秋。冬)。
這個(gè)時(shí)候。你寫四個(gè)字符串常量
1
2
3
4
5
6
|
public class common { public static final string spring= "春" ; public static final string season= "夏" ; public static final string summer= "秋" ; public static final string autumn= "冬" ; } |
在get方法里放入get(common.season),確實(shí)是把"春",放進(jìn)去了,但是這個(gè)時(shí)候你會(huì)發(fā)現(xiàn)這里面有一個(gè)隱患,你get(string season),畢竟放入的是string類型的,如果新同事或者不知情的同事,不知道這個(gè)方法里只能放“春、夏、秋、冬”,它放了個(gè)其期它字符串比如get("小小“),這個(gè)時(shí)候,在編譯期它是不會(huì)報(bào)錯(cuò)的,只有運(yùn)行之后,才發(fā)現(xiàn)錯(cuò)了。
為了防止上面的隱患,枚舉出現(xiàn)了
1
2
3
4
5
6
7
|
public enum season { spring( "春" ), summer( "夏" ), autumn( "秋" ), winter( "冬" ); ..... } |
這個(gè)時(shí)候,我們修改get方法的傳參,改成get(season season) 這個(gè)時(shí)候加入get(season.spring),這就能保證傳入的參數(shù)只能是這幾個(gè)。
二.理解枚舉
首要我們要明確,其實(shí)枚舉也是個(gè)class類,我寫個(gè)枚舉來理解。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
//我們把枚舉當(dāng)做一個(gè)普通類 public enum season { spring( 1 , "春" ), summer( 2 , "夏" ), autumn( 3 , "秋" ), winter( 4 , "冬" ); //這里最后一個(gè)一定要分號(hào),否則報(bào)錯(cuò) /*我們可以理解成 *public static final season spring = new season(1,春); *public static final season summer = new season(2,夏); *public static final season autumn = new season(3,秋); *public static final season winter = new season(4,冬); *既然是對(duì)象,那下面就很好理解了 */ /* * 1.上面對(duì)象里放了兩個(gè)參數(shù),那下面就肯定要有這個(gè)類型的構(gòu)造函數(shù) * 2.這里是private,因?yàn)椴荒茉诒籲ew對(duì)象了 */ private season( int code,string name) { this .name = name; this .code = code; } //對(duì)象的屬性 private string name; private int code; //獲取對(duì)象屬性的方法 public string getname() { return this .name; } public string getcode() { return this .name; } //通過code獲得對(duì)象,我們就可以獲得對(duì)象的其它屬性 public static season decode( int code) { season season = null ; for (season type : season.values()) { if (type.code==code) { season = type; break ; } } return season; } //重新tostring方法 public string tostring() { return this .name; } } |
上面這個(gè)例子,就很好解釋了枚舉,它和普通類沒什么區(qū)別,只是用另一種寫法創(chuàng)建了幾個(gè)有屬性的對(duì)象,這也必須寫這樣有屬性的構(gòu)造函數(shù),僅此而已。
這里順便列舉下枚舉的一些特點(diǎn):
1.它不能有public的構(gòu)造函數(shù),這樣做可以保證客戶代碼沒有辦法新建一個(gè)enum的實(shí)例。
2. 枚舉不能在繼承其它類了,因?yàn)樗J(rèn)繼承了java.lang.enum
3. 常量值地址唯一,可以用==直接對(duì)比,性能會(huì)有提高.
4.enum還提供了values方法,這個(gè)方法使你能夠方便的遍歷所有的枚舉值。
5.enum還有一個(gè)oridinal的方法,這個(gè)方法返回枚舉值在枚舉類種的順序,這個(gè)順序根據(jù)枚舉值聲明的順序而定。
三.枚舉的常見用法
第一種:switch運(yùn)用
先建一個(gè)枚舉:
1
2
3
4
5
6
|
public enum common { insert, modify, delete } //因?yàn)檫@里是無參的對(duì)象,所以可以用系統(tǒng)默認(rèn)的構(gòu)造函數(shù)。也不用寫屬性和方法。 |
在寫實(shí)現(xiàn)代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
public class commonutils { public static void gettype(common common){ common c=common; switch (c) { case insert: system.out.println( "進(jìn)行插入操作" ); break ; case modify: system.out.println( "進(jìn)行修改操作" ); break ; case delete: system.out.println( "進(jìn)行刪除操作" ); break ; } } public static void main(string[] args) { gettype(common.delete); //后臺(tái)輸出:進(jìn)行刪除操作 } } |
第二種用法,通過key值獲得value值獲取其它值
枚舉類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
public enum season { spring( 1 , "春" , "春天放風(fēng)箏" ), summer( 2 , "夏" , "夏天去游泳" ), autumn( 3 , "秋" , "秋天去秋游" ), winter( 4 , "冬" , "冬天吃火鍋" ); private season( int code,string name,string bz) { this .code = code; this .name = name; this .bz=bz; } private int code; private string name; private string bz; public static season decode( int code) { season season = null ; for (season type : season.values()) { if (type.code==code) { season = type; break ; } } return season; } public int getcode() { return code; } public string getname() { return name; } public string getbz() { return bz; } } |
測(cè)試類
好了,就寫這么多,以后有需要會(huì)更深入了解。感謝大家對(duì)服務(wù)器之家的支持。
原文鏈接:https://www.cnblogs.com/qdhxhz/p/8337514.html