as3并不支持枚举,要想用枚举只能自己定义来模仿类似枚举的效果,来封装一组值。
可以定义一个包含一些常量的类来定义枚举,如
public final class Day{
public static const MONDAY:Day = new Day();
public static const TUESDAY:Day = new Day();
public static const WEDNESDAY:Day = new Day();
public static const THURSDAY:Day = new Day();
public static const FRIDAY:Day = new Day();
public static const SATURDAY:Day = new Day();
public static const SUNDAY:Day = new Day();
public static const TUESDAY:Day = new Day();
public static const WEDNESDAY:Day = new Day();
public static const THURSDAY:Day = new Day();
public static const FRIDAY:Day = new Day();
public static const SATURDAY:Day = new Day();
public static const SUNDAY:Day = new Day();
}
public final class Fruit
{
public static const APPLE:String =
“apple”;
public static const BANANA:String =
“banana”;
public static const ORANGE:String =
“orange”;
}
另外一种方法是殿堂之路里面介绍的,是用object来存储键-值组,通过访问键来获取相应的值如下:
var directions:object={Left:0,ight:1,up:2,down:3};
var direction:Number=directions.Left;
使用object键值组比较方便,减少很多代码,其形式更接近枚举。