注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

神魔破杜梓的叨叨堂

Programming every day!

 
 
 

日志

 
 
 
 

Static 关键字的新玩法  

2008-05-16 23:13:07|  分类: My Tech |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
本文来自InsideRIA

One of the most popular posts on my blog adamflater.net has been Static Code Blocks!. Due to the amount of requests for that post I thought it made for a good topic to feature on InsideRIA regarding ActionScript and static code.

First off, what is static code? Static code is executable code that is said to belong to a Class. An instance method of an Object exists N times for the N instances of the Class type of Object, but static code exists only once no matter how many instances of the Object exist. The other difference between an instance method and static code is that static code has a static scope. In other words, the code executing in the static block may only access the other static members of the Class that it belongs to.

There are essentially three ways to create static code blocks in ActionScript: Using the Mixin Class meta tag, wrapping the code in an unnamed block, and using a static method initializer pattern. In reviewing each of these solutions it's important to consider order of execution and scope.

So, let's take a look at these three solutions in the order that they're executed.

1. Using a static var as an initializer

This solution is executed by the Flash Player first out of the other two solutions. It involves using a static variable to call a static method.

Generally it looks like this:

 
private static var initialized : Boolean = initializer();
private static function initializer() : Boolean
{
trace( "static method init'er" );
return true;
}

In this example initialized is a static variable that technically belongs to the Class (not the Object). Although this variable is accessible by the Object it "lives" at the Class level. When the class is being loaded by the Flash Player the variable initialized is created and assigned the value returned by the initializer method. Like the other examples, this code is executed once no matter how many times the Class it belongs to is instantiated.

2. Unnamed code block

If, for some reason, you're using the static initializer pattern shown in the first example and you also have an unnamed code block in your class, the unnamed code block will be executed second. A simpler example is:

 
{
trace( "this is a static block" );
}

Again, the scope of this block is the static scope (ie Class members are accessible).

3. The Mixin meta tag

The last to execute in this series of static code blocks is the Mixin meta tag. An example of this solution is:

 
[Mixin]
public class StaticBlocks
{
public static function init( systemManager : ISystemManager ) : void
{
trace( "mix-in init method" );
}
...

In this solution the init method also has a static scope limited to the Class it belongs to, but in addition there is a reference passed into the method to the application's SystemManager called systemManager. The system manager provides access to many interesting areas of your application. SystemManager is also available via the static reference: Application.application.systemManager however, in the previous two examples the classes needed to obtain a reference the SystemManager using Application.application.systemManager have not yet been created. By using the Mixin tag you're guaranteed that the Flash Player / Flex Framework has initialized and created the Application and SystemManger classes.

It's equally important to note that the other two solutions executed before the Application and SystemManager have finished initializing. This is important if you have some code that you need to have executed before these classes finish their instantiations.

Aside from initializing custom styles in components you may not find a lot of uses for static code blocks in a typical Flex application, but these techniques can come in handy if you're building a service API or Framework in Flex.

I hope this was a valuable follow up post to my original effort outline static code blocks. I'd love to hear your questions and comments.


  评论这张
 
阅读(396)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017