“作为"有什么意义?此代码中的关键字 [英] What's the significance of "as" keyword in this code

查看:34
本文介绍了“作为"有什么意义?此代码中的关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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();
        }
    }
}

推荐答案

这是 ActionScript 3 中引入的强制转换运算符.与 as 的区别(相对于 Type(object)casting) 是如果转换失败,结果将是类型的默认值.更多关于这个这里

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

这篇关于“作为"有什么意义?此代码中的关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆