declare members for dynamic class(翻译)

参考地址:

http://www.adobe.com/cfusion/communityengine/index.cfm?event=showdetails&productId=2&postId=13066

PROBLEM SUMMARY
需要为动态的类(dynamic class)声明属性,但有可能与类本身内建的属性冲突

SOLUTION SUMMARY
在自定义的命名空间(custom namespace)中声明新成员。

EXPLANATION
动态类(dynamic class)允许使用在运行时为其添加属性和方法,这样新添的成员就可能与类内建的成员发生冲突。
比如:
package{
      import flash.display.Sprite;
      public dynamic class DynamicObject extends Sprite{
            public function DynamicObject():void{
                  super();
            }
            public function someName():void{
                  trace(‘method’);
            }
      }
}
我们不能再给DynamicObject类添加”someName”属性。下面的代码会引起编译错误”1168: Illegal assignment to function someName. “
var obj:Object = new DynamicObject();
obj.someName = “value”;

如果使用namespace,问题迎刃而解。
member_ns.as
package {
      public namespace member_ns = ‘urn:some-uri’;
}

DynamicObject.as
package{
      import flash.display.Sprite;
      public dynamic class DynamicObject extends Sprite{
            public function DynamicObject():void{
                  super();
            }
            member_ns var property:Boolean = true;
            member_ns function someName():void{
                  trace(‘method’);
            }
      }
}
可通过namespace访问这些方法,只需要指明所要引用的成员对象的namespace:
var object:DynamicObject = new DynamicObject();
object.someName = “value”;
object.member_ns::someName(); // method
      trace(object.member_ns::property); // true

这一招在写Proxy子类的时很管用。

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: