-
Notifications
You must be signed in to change notification settings - Fork 0
Enums
Enums is a language extension that adds an enumerated type support to ActionScript3.
The enumerated type consist of a set of named values. For example, the four suits in a deck of playing cards may be four enumerators named SPADES
, CLUBS
, DIAMONDS
, HEARTS
, belonging to an enumerated type named Cardsuit
.
##Syntax
Enum class declaration can be created the from the right-click menu on the package after importing the enum language to a project. The syntax is similar to a class declaration statement. Enumerated members should be written in capital letters and separated by commas.
public enum Cardsuit extends <none> implements <none> {
SPADES,
CLUBS,
DIAMONDS,
HEARTS
public function Cardsuit() {
// enum constructor
}
}
Enum constructor, as well as any other function, can receive arguments. Besides the constructor, an enumerator can have another methods, as well as any other class.
public enum Cardsuit extends <none> implements <none> {
SPADES(13),
CLUBS(13),
DIAMONDS(13),
HEARTS(13)
public function Cardsuit(amount : int) {
// this will be executed on instance creation
_amount = amount; // create field and assign a parameter
}
private var _amount : int;
public function get amount ( ) : int {
// getter
return _amount;
}
}
An enumerator can be referred from another class using an expression, containing enumerated type name, enumerator and its properties.
public function Main() {
Cardsuit.SPADES;
Cardsuit.CLUBS.amount;
}
Name operation is often used when it needs to get enumerators from any other method.
public function Main() {
myGetEnum(Cardsuit.DIAMONDS);
}
public function myGetEnum(myEnum : Cardsuit) : void {
myEnum.name; // myEnum.name = DIAMONDS
myEnum.amount; // DIAMONDS.amount value
}
Cardsuit.NAMES; // list, that stores all enums' name of Cardsuit class
Using switch statement is a good practice with enums, because case expression allows to use short form to refer to an enumerator.
switch (myEnum) {
case SPADES :
break;
case CLUBS :
break;
case DIAMONDS :
break;
case HEARTS :
break;
}