开发者

ActionScript dynamic type?

I am curious about code in my ActionScript project:

public var _p:Object 
...

in a function:

publ开发者_如何学JAVAic function WObject(pp:MovieClip)
{
    _p = pp;
    _p.Play();
}

The Play() function is in MovieClip but not Object; can ActionScript allow such syntax?


Since any AS3 object is a child of Object, downcasting a MovieClip to Object is possible. This is possible due to the dynamic nature of AS3 objects (since AS3 is based on ECMAScript) which would allow you to invoke methods such as play() on an Object that contains a MovieClip without having the compiler/player throw sharp objects at you.

Having said the above, it is generally a good practice not to downcast in such a manner unless absolutely necessary since this tends to make code harder to understand and thus maintain.

Other considerations are compile-time type checking and code-hinting (argument hints, code completion, etc). These would not be available to you should you choose to downcast to Object.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜