枚举类型

      网摘 2006-4-20 6:3
摘自:http://www.darronschall.com/weblog/archives/000097.cfm

/* A simple way to create enumerated types in ActionScript. Just
  provide a list of string arguments to the constructor and
  the strings will become identifiers in the instance.
*/
class com.darronschall.weblog.EnumeratedType {
  
public function EnumeratedType() {
    for (var i = 0; i < arguments.length; i++) {
      // set up the identifier in the instance, and give
      // it a numerical value, unique to the instance
      this[arguments[i]] = i;
    }
  }
}
//You would use it in your scripts like this:

///////////////////////////////////////////////
import com.darronschall.weblog.EnumeratedType;

// create an enumeration for the days of the week
days = new EnumeratedType("Sunday", "Monday", "Tuesday", "Wednesday",
             "Thursday", "Friday", "Saturday");

// the line below much easier to read than:
//    today = 4;
// because the meaning of the code is clear
// just by looking at it
today = days.Thursday;

// we can do neat things with enumerations as well,
// like use them as loop counters
for (var day = days.Sunday; day <= days.Saturday; day++) {
  if (day == today) {
    trace("Found a match.. day == today == days.Thursday");
  }
}

// or even in a switch statement
tomorrow = days.Friday;
switch(tomorrow) {
  case days.Monday:
    trace("Monday.. back to work.");
    break;

  case days.Friday:
    trace("Alright! Tomorrow is Friday!");
    break;
}
标签集:TAGS:
回复Comments() 点击Count()

回复Comments

{commentauthor}
{commentauthor}
{commenttime}
{commentnum}
{commentcontent}
作者:
{commentrecontent}