考虑一个.as
文件,如下所示:
package foo {
class Foo {
public static var a:* = getA();
public static var b:* = getB();
public var c:* = getC();
public static function d():* { ... }
public function Foo() {
trace("initializing");
}
}
}
// internal functions/variables
var e:* = getD();
function f():* { ... }
初始化每个变量/函数a..f
的定义顺序是什么?
(我知道我可以做实验来找出......但我正在寻找实际的指定定义)
答案 0 :(得分:2)
嗯,棘手的部分是静态初始化。
在执行任何执行之前声明所有静态方法和变量。静态方法是“即时”初始化的,在某种意义上你可以随时调用它们。然后按照外观的顺序初始化静态变量。然后按照外观顺序执行所有静态初始化:
package {
import flash.display.Sprite;
public class Main extends Sprite {
__init(c,'static');//"static initialization b" because all variables are initialized prior to static initialization
public static var a:String = __init(b);//"variable initialization null", because b is declared, but not initialized
public static var b:String = __init('b');//"variable initialization b" for obvious reasons
public static var c:String = __init(b);//"variable initialization b" because b is now initialized
public static function __init(s:String, type:String = 'variable'):String {
trace(type, 'initialization', s);
return s;
}
public function Main():void {}
}
}
但总的来说,我的建议不是依赖它,除非你真的必须这样做。