AS3静态代码块的初始化使用方法
2014-01-26 13:47
288 查看
In a nutshell: A static initializer is executed whenever you do anything with that class. It's executed
before whatever you wanted to do (e.g. calling the constructor or accessing a field). It's also only executed once.
Many moons ago I released some code which utilized a static initializer. That code worked fine back then, but recent versions of the Flex SDK compiler don't really like it. Well, to tell the truth I
also didn't like it, because the construct I used was sorta ugly and, well, pretty wrong.
The hello world of static initializers looks like this:
Declaring variables there or using loops doesn't work, however. Loops used to work, but the proper way to handle this is better anyways. All you need is an anonymous function which is invoked directly:
AS3 has function scope just like JavaScript. This means the declared variables are available within the function they were declared. So, we can use some temporary objects/variables and they will be discarded as soon as we're done with this initialization
stuff. They won't waste memory and they also won't clutter up this class' namespace.
OtherClass.as
Output:
before whatever you wanted to do (e.g. calling the constructor or accessing a field). It's also only executed once.
Many moons ago I released some code which utilized a static initializer. That code worked fine back then, but recent versions of the Flex SDK compiler don't really like it. Well, to tell the truth I
also didn't like it, because the construct I used was sorta ugly and, well, pretty wrong.
The hello world of static initializers looks like this:
//static (this comment isn't required, but I recommend using one) { trace('woo! static!'); }
Declaring variables there or using loops doesn't work, however. Loops used to work, but the proper way to handle this is better anyways. All you need is an anonymous function which is invoked directly:
//static { (function():void { var i:int; for (i = 0; i < 3; i++){ trace(foo + i); } }()); }
AS3 has function scope just like JavaScript. This means the declared variables are available within the function they were declared. So, we can use some temporary objects/variables and they will be discarded as soon as we're done with this initialization
stuff. They won't waste memory and they also won't clutter up this class' namespace.
A Complete Example
HelloStatic.aspackage { import flash.display.*; public class HelloStatic extends Sprite { //static { trace('hello'); } public function HelloStatic():void { trace('world'); trace(OtherClass.field); } } }
OtherClass.as
package { public class OtherClass{ public static var field:String ='not initialized yet'; //static { field = 'initialized'; } } }
Output:
hello world initialized
相关文章推荐
- Java中静态代码块,静态变量,静态方法,构造方法初始化顺序
- java静态方法,静态初始化块及静态变量的声明和初始化不能使用泛型的原因
- Java中非静态代码块,静态代码块和构造方法的初始化顺序
- 静态类、静态方法、静态代码块及初始化顺序
- java中类/对象的初始化顺序以及静态代码块的使用
- java--面向对象(static关键字、main、静态什么时候使用、静态代码块、对象的初始化过程)
- java 程序加载过程---3--类中申明同时申明类的静态对象 创建类的实例 访问类的静态变量 调用类的静态方法 使用反射方法 初始化类的子类对象 直接使用java.exe 调用某个类
- java中类的静态代码块、构造代码块、构造方法、静态成员的初始化顺序详解
- Java类的各种成员初始化顺序如:父子类继承时的静态代码块,普通代码块,静态方法,构造方法,等先后顺序
- java中的静态变量、静态方法与静态代码块详解与初始化顺序
- 多线程编程 使用 synchronized来同步静态方法 非静态方法 代码块
- java中类/对象的初始化顺序以及静态代码块的使用
- java 继承时子类父类的静态非静态成员变量,构造方法,静态代码块的初始化顺序
- Java中的static使用之静态变量、静态方法和静态初始化块
- java中类/对象的初始化顺序以及静态代码块的使用
- java类加载和实例化:静态代码块、初始化代码块、构造方法的执行顺序
- Java类的各种成员初始化顺序如:父子类继承时的静态代码块,普通代码块,静态方法,构造方法,等先后顺序
- [AS3]在反射中使用静态方法
- java中类/对象的初始化顺序以及静态代码块的使用
- java中类的静态代码块、构造代码块、构造方法、静态成员的初始化顺序详解