Summary on 20080616: Basic usage of Enum in Java1.5

The type Enum is imported by Java1.5, here are some of the notice about using Enum in Java

1. Enum is actually one interface

2. Consider Enum as one simple class with several pre-defined instance, so user can define his own variable and method in Enum. Also Enum can implement more than one interface.

3. user can define his own constructor in Enum, but note than do not define any public constructor, otherwise, it will expose interface for other to define Enum. But Enum is pre-defined.

4. Enum implements the Comparable interface by default

5. all the enum value is defined as public static final by default

6. in Enum, the value list should be defined at the first place

7. values() method return all the enum values, and oridinal() method return the order of one enum value

8. user can define seperate method for each enum value

for example:

public enum SeperateMethodEnum {

	Spring{

		public String toString(){

			return "Spring";

		}

	},

	Summer;

         public String toString(){

                  return “toString”;

         }

}

9. for enum value, user can define value attached to it

for example:

public enum ValueEnum {

    Spring(1),

    Summer(2),

    Automn(3),

    Winter(4);

    

    ValueEnum(int value){

    	this.value = value;

    }

    

    private int value;

    

    public int getValue(){

    	return value;

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值