简体   繁体   中英

create enum in actionscript 3 and compare

I need to create some enum values, give it a default value and then compare it.

I have this enum class

public class Car
{
    public static const Tesla:int = 1;
    public static const Ford:int = 2;
}

How do I initiate a new Car enumn variable with a default value of "Tesla" and how do I compare the variable? I'm looking for something like this:

public var c:Car = new Car(Car.Tesla);

if (c == Car.Tesla){
// Do something
}

Edit, it is now changed to the following:

public final class Car
{

    public static const Tesla:String = "tesla";
    public static const Ford:String = "ford";

}

And in the mxml file:

    public var c:String = Car.Tesla;

    if (c == Car.Tesla){
        // Do something
    }

I have this enum class

Just so we're on the same page about it: that's not an enum and there are no enums in as3. The language doesn't have that feature.

How do I initiate a new Car enumn variable with a default value of "Tesla" and how do I compare the variable?

You cannot, because Car is a type and the static properties it has are of type int which is something completely different.

What you can do is this:

var c:int = Car.Tesla;

if (c == Car.Tesla){
    // Do something
}

If you want to have a Car object instead, add a brand property to the class of type int , which you can then assign the value of your constants to:

var c:Car = new Car();
c.brand = Car.Tesla;

if (c.brand == Car.Tesla){
    // Do something
}

You could also add a parameter to the constructor and insert the value there.

Btw. changing

public static const Tesla:int = 1;

to

public static const Tesla:String = "tesla";

will give you the chance to get more meaningful values during debugging. The built in constants like MouseEvent.CLICK are defined this way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM