forked from: 構造体もどきの変数の初期化方法

by aaharu forked from 構造体もどきの変数の初期化方法 (diff: 25)
this使ったほうがいい。
♥0 | Line 43 | Modified 2011-01-04 23:01:47 | MIT License
play

ActionScript3 source code

/**
 * Copyright aaharu ( http://wonderfl.net/user/aaharu )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/aKvb
 */

// forked from aduka's 構造体もどきの変数の初期化方法
package {
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
        public function FlashTest() {
            // write as3 code here..
            
            //     構造体のテスト用オブジェクト
            var test:Test = new Test( 14, 234.54354 );
            
            //    出来るのなら14, 243.5354と表示されて欲しい
            this.addChild( test.display() );
        }
    }
}

import flash.text.TextField;
import flash.display.Sprite;

// C++で言うところの構造体もどき
class Test{
    private var _index:uint;
    private var _value:Number;
    
    public function get index():uint
    {
        return _index;
    }
    
    public function set index(n:uint):void
    {
        _index = n;
    }
    
    public function get value():Number
    {
        return _value;
    }
    
    public function set value(num:Number):void
    {
        _value = num;
    }
 
    // 初期化演算子が見当たらないので代入演算子でテスト
    public function Test( index:uint, value:Number ){
        _index = index;
        _value = value;
    }
    
    public function display():Sprite{
        var sp:Sprite = new Sprite();
        var tf:TextField = new TextField();
        var text:String = "Index: " + index + ", value: "  +value;
        tf.text = text;
        sp.addChild( tf );
        return sp;
    }


}