列舉的使用

2021-08-18 01:58:01 字數 1036 閱讀 9119

什麼是

列舉?

需要在一定的範圍內取值,這個值只能是這個範圍中的任意乙個。

舉例:交通訊號燈,有三種顏色,但是每次只能亮三種顏色裡面的任意乙個。

列舉關鍵字

enum

enum color3
列舉的構造方法也是私有的

列舉的使用方法示例:

package test04;

public class testenum1

//傳統的方式

private int color;

//第二種方法

private color2 color1;

//第三種方式,jdk5.0新特性 使用列舉

private color3 color3;

public void test() }

//使用列舉實現

enum color3//列舉也是底層也是私有的,只是寫起來更方便

//特殊列舉

enum color4

}, green("green")

}, yellow("yellow")

}; //列舉裡的構造方法

private color4(string name) {}

//抽象方法

//當在列舉裡寫了抽象方法之後,需要在每個示例上面都實現抽象方法

public abstract void print1(); }

class color2

public static final color2 red = new color2();

public static final color2 green = new color2();

public static final color2 yellow = new color2();

}class color1

特殊列舉的操作:

在列舉類裡面有構造方法

在列舉類裡面有抽象方法

列舉的使用

列舉的使用 include define pink 0 define red 1 define green 2 define white 3 define black 5 define yellow 6 enum是關鍵字 裡面的成員是乙個識別符號,列舉常量 列舉型別 成員 列舉成員,列舉常量 enu...

列舉的使用

實體 private canci name enumerated enumtype.string column name name nullable false,length 10 public canci getname public void setname canci name public ...

列舉的使用

123 4 一 列舉類的使用 5 1.列舉類的理解 類的物件只有有限個,確定的。我們稱此類為列舉類 6 2.當需要定義一組常量時,強烈建議使用列舉類 7 3.如果列舉類中只有乙個物件,則可以作為單例模式的實現方式。8 9 二 如何定義列舉類 10 方式一 jdk5.0之前,自定義列舉類 11 方式二...