As3 simulation class Enumeration

Source: Internet
Author: User

"Enumeration" is a set of custom data types that you have created to encapsulate a group of values. ActionScript 3.0 does not support specific enumeration tools, which are used with C ++.Enum
The keyword or Java uses the enumeration interface is different. However, you can create an enumeration using a class or a static constant. For example, the printjob class in ActionScript 3.0 uses the enumeration name printjoborientation to store"Landscape"
And"Portrait"
A group of values, as shown in the following code:

public final class PrintJobOrientation 
{
public static const LANDSCAPE:String = "landscape";
public static const PORTRAIT:String = "portrait";
}

By convention, enumeration classes are usedFinal
Attribute declaration, because the class does not need to be extended. This class only consists of static members, which means that the instance of this class is not created. Instead, you can directly access enumeration values through class objects, as shown in the following code excerpt:

var pj:PrintJob = new PrintJob(); 
if(pj.start())
{
if (pj.orientation == PrintJobOrientation.PORTRAIT)
{
...
}
...
}

All enumeration classes in ActionScript 3.0 only contain string, Int, or uint
Type variable. The advantage of enumeration instead of text strings or numeric values is that enumeration is easier to identify literal errors. If the enumeration name is incorrect
The compiler generates an error. If you use a literal value, the compiler does not report any spelling errors or incorrect numbers. In the previous example, if the name of the enumerated constant is incorrect, the compiler generates an error
Error, as shown in the following code excerpt:

    if (pj.orientation == PrintJobOrientation.PORTRAI) // compiler error

However, if the character string nominal value is misspelled, the compiler does not generate an error, as shown below:

    if (pj.orientation == "portrai") // no compiler error

The second way to create an enumeration is to create a separate class using the static properties of the enumeration. The difference between this method is that each static attribute contains a class instance, rather than a string or integer. For example, the following code creates an enumeration class for each day of a week:

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();
}

This method is not used in ActionScript 3.0, but many developers prefer the improved type check feature provided by this method. For example, the return value can be limited to the enumerated data type. The following code not only shows the functions returned every day of the week, but also shows the function calls that use the enumeration type as the type annotation:

function getDay():Day 
{
var date:Date = new Date();
var retDay:Day;
switch (date.day)
{
case 0:
retDay = Day.MONDAY;
break;
case 1:
retDay = Day.TUESDAY;
break;
case 2:
retDay = Day.WEDNESDAY;
break;
case 3:
retDay = Day.THURSDAY;
break;
case 4:
retDay = Day.FRIDAY;
break;
case 5:
retDay = Day.SATURDAY;
break;
case 6:
retDay = Day.SUNDAY;
break;
}
return retDay;
}

var dayOfWeek:Day = getDay();

You can also enhance the function of the day class to associate an integer with each day of the week, and provideTostring ()
Returns the string representation of each day. You may want to use this method to enhance the function of the day class.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.