Ok, let's spill some light on the flow of things with constants. I devised a pretty simple and self-explanatory class:
package
{
import flash.display.Sprite;
public class Kunst extends Sprite
{
public const A:int = int(trace("Init A:", A, B, C, D)) + 10;
public const B:String = initB() + String(trace("Init B:", A, B, C, D)) + initB();
public const C:int = 10 + 10;
static public const D:String = String(trace("Init D:", D, E, F)) + initD();
static public const E:Boolean = initE() && Boolean(trace("Init E:", D, E, F)) || initE();
static public const F:int = 10 + 10 + 10;
public function Kunst()
{
super();
trace("Check ABC:", A, B, C);
trace("Check DEF:", D, E, F);
}
private function initB():String
{
trace("Init B2:", A, B, C, D);
return "B";
}
static private function initD():String
{
trace("Init D2:", D, E, F);
return "D";
}
static private function initE():Boolean
{
trace("Init E2:", D, E, F);
return true;
}
}
}
It outputs the following:
Init D: null false 30
Init D2: null false 30
Init E2: undefinedD false 30
Init E: undefinedD false 30
Init E2: undefinedD false 30
Init A: 0 null 20 undefinedD
Init B2: 10 null 20 undefinedD
Init B: 10 null 20 undefinedD
Init B2: 10 null 20 undefinedD
Check ABC: 10 BundefinedB 20
Check DEF: undefinedD true 30
Lets comprehend the results:
- All static constants go before the very first line of the application code executes. Before non-static constants of the main document class as well. That all was expected.
- Among the same ones (i.e. either static or non-static) first initialize those that can be calculated right away, ones with outright value or simple formula. They, actually, could even be defined at the compile time.
- The rest, the ones within the same (static or non-static) group, go by the exact order they are declared.
- I thought you can initialize constants in the constructor block, but it turns out you can not. Maybe it was so in AS2, I don't really remember now.
- At the moment the constructor starts executing, all the class member constants are already initialized.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…