2006/04/20 | 枚举类型
类别(网摘) | 评论(0) | 阅读(26) | 发表于 06:03
摘自: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;
}
0

评论Comments

日志分类
首页[28]
flash[3]
javascript[3]
C#[1]
feeling[5]
网摘[16]