这可能在AS3中吗?

时间:2015-05-09 07:34:44

标签: actionscript-3

班级Color

public class Color {

    public static const Black:uint = 0x000000;
    public static const White:uint = 0xffffff;

    public static const Red:uint = 0xff0000;
    public static const Blue:uint = 0x0000ff;
    public static const Green:uint = 0x008000;

    public var code:uint;
    public var family:String;

    public function Color():void {}

}

Main.as

public class Main extends Sprite 
{

    public function Main() {
        var red:Color = new Color();
        red.code = Color.Red;
        trace(red);
    }

如何使trace(red)输出0xff0000

1 个答案:

答案 0 :(得分:3)

为该课程覆盖toString()

override public function toString():String {
    return "Ox"+code.toString(16);
}

旁注:您的public function Color()不应该返回void,而是保留不返回类型。此外,red中的Main变量未初始化。代码应为var red:Color=new Color();