我們可以在 Java 中擴充套件列舉嗎?
否,我們不能擴充套件 Java 中的列舉。Java 列舉可以隱式擴充套件java.lang.Enum類,因此列舉型別不能擴充套件其他類。
語法
public abstract class Enum> implements Comparable, Serializable { // some statements }
列舉
- 列舉型別是一種特殊資料型別,新增到 Java 1.5 版本中。
- 列舉用於定義常量集合,當我們需要預定義值列表且它們不表示某種數字或文字資料時,可以使用列舉。
- 列舉是常量,並且預設情況下它們是靜態的和最終的。因此列舉型別欄位的名稱以大寫字母表示。
- 公共或受保護的修飾符只能與頂級列舉宣告一起使用,但所有訪問修飾符都可以與巢狀列舉宣告一起使用。
示例
enum Country { US { public String getCurrency() { return "DOLLAR"; } }, RUSSIA { public String getCurrency() { return "RUBLE"; } }, INDIA { public String getCurrency() { return "RUPEE"; } }; public abstract String getCurrency(); } public class ListCurrencyTest { public static void main(String[] args) { for (Country country : Country.values()) { System.out.println(country.getCurrency() + " is the currecny of " + country.name()); } } }
輸出
DOLLAR is the currecny of US RUBLE is the currecny of RUSSIA RUPEE is the currecny of INDIA
廣告