構造体もどきの変数の初期化方法
C++で言うところのhttp://ideone.com/txCVjをやってみたかった
AS3だと初期化演算子が見当たらないので、変数(プリミティブ型)の初期化方法を代入演算子で代用したところ痛い目を見た例
thisを使う以外に何か方法とか無いのかな?
♥0 |
Line 27 |
Modified 2011-01-04 10:15:43 |
MIT License
archived:2017-03-10 10:25:46
ActionScript3 source code
/**
* Copyright aduka ( http://wonderfl.net/user/aduka )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/wJtx
*/
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{
public var index:uint;
public var value:Number;
// 初期化演算子が見当たらないので代入演算子でテスト
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;
}
}