Java基礎:列舉Enum
在某些情況下,一個類的物件是有限而且固定的,比如季節類,它只有4個物件:春、夏、秋、冬。這種例項有限而且固定的類,在Java裡被稱為列舉類。
1 手動實現列舉類
示例:
?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 |
package com.demo2;
public class Season {
public static final Season SPRING = new Season( "春天" , "趁春踏青" );
public static final Season SUMMER = new Season( "夏天" , "夏日炎炎" );
public static final Season FALL = new Season( "秋天" , "秋高氣爽" );
public static final Season WINTER = new Season( "冬天" , "圍爐觀雪" );
public
|