What's the significance of “as” keyword in this code

回眸只為那壹抹淺笑 提交于 2019-12-13 01:29:07

问题


package
{
    import flash.display.Sprite;
    import flash.media.Sound;
    import flash.media.SoundChannel;

    public class EmbeddedSoundExample extends Sprite
    {
        [Embed(source="smallSound.mp3")]
        public var soundClass:Class;

        public function EmbeddedSoundExample()
        {
          //WHAT DOES "as" keyword DO IN THE FOLLOWING LINE ??
          //*************************************************

            var smallSound:Sound = new soundClass() as Sound;

            //COULD BE WRITTEN AS : 
            //==>>>> var smallSound:Sound = new soundClass() ???
            // OR
            ////==>>>> var smallSound:Sound = new Sound() ???
         //******************************************************

            smallSound.play();
        }
    }
}

回答1:


This is a casting operator introduced in ActionScript 3. The difference with as (as opposed to Type(object) casting) is that if the cast fails, the result will be the default value for the type. More on this here




回答2:


as casting, use the data like the Type that your are using, no change the data. if you use Type(object) and you can't convert to this Type, return a default value( null,zero, or undefined)



来源:https://stackoverflow.com/questions/12390347/whats-the-significance-of-as-keyword-in-this-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!