Created
January 31, 2012 11:00
-
-
Save DavidArno/1709906 to your computer and use it in GitHub Desktop.
AS3 enum example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Enum base class: | |
package language.extensions.enums | |
{ | |
public class EnumBase | |
{ | |
function EnumBase(token:*) | |
{ | |
if (token !== runtimeInstantiationPreventionKey) | |
{ | |
throw new Error("Cannot create new enums at runtime."); | |
} | |
} | |
static protected function key():* | |
{ | |
return runtimeInstantiationPreventionKey; | |
} | |
} | |
} | |
const runtimeInstantiationPreventionKey:* = {}; | |
And a typical enum would be: | |
package ... | |
{ | |
public final class TrafficLights extends EnumBase | |
{ | |
public static const Red:TrafficLights = new TrafficLights(key); | |
public static const Amber:TrafficLights = new TrafficLights(key); | |
public static const Green:TrafficLights = new TrafficLights(key); | |
function TrafficLights(key:*) | |
{ | |
super(key); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment