AS3中的强制类型转换 AS3 in the compulsory conversion
07月29th, 2007 — Dreamer 07月29 th, 2007 - Dreamer每一种编程语言都提供强制类型转换,允许你将某一种数据类型转换成另一种数据类型,AS3自然也不例外。 Each provides a programming language compulsory conversion, allows you a certain kind of data will be converted into another type of data types, AS3 is no exception. 但是虽然我编写了不少Flex程序,对AS3中的强制类型转换还是不太清楚,以前AS中是这样进行强制类型转换的:假设有一个类叫做Class1,我们声明了一个它的对象c1,如果想要将它转换成Class2类型,只要这样写: But while I prepared a lot of Flex procedures, AS3 in the mandatory conversion or is not very clear, before AS in this type of mandatory conversion: the assumption that there is a category called Class1, we have a statement of its target c1, if It wants to convert Class2 type, as long as read:
Class2(c1); Class2 (c1);
在AS3中你依然可以这样写,但是AS3 中提供了一个新的操作符: as ,并且推荐使用as 进行强制转换,上述的例子用as 操作符实现就是这样: In AS3 in this way you can still write, but AS3 provide a new operator: as, and recommended as a mandatory conversion, the above examples used as operators realize is this:
c1 as Class2; c1 as Class2;
使用as 操作符有几个好处: Use as the operator has several advantages:
1.它的效果和第一种方法是一样的。 1.'s Effectiveness and its first approach is the same.
2.如果类型不兼容无法转换,就会返回null,而不是出错。 2. If the type is not compatible can not be converted, it will return null, and not an error. 这样你就可以自定义错误的时候该做什么。 So you can customize the wrong time to do.
3.没有运行时错误(Run Time Error)提示。 3. Is not running at the wrong (Run Time Error) tips.
不过有些时候我在使用as 的时候并不能达到强制转换的目的,而使用第一种方法则可以。 But sometimes I use as the time and can not achieve the purpose of forced conversion, and the use of a method can. 为什么as 操作符有时候会不好用呢? Why as operators sometimes do not use » 这个问题困扰了我很久,知道昨天在MXNA上发现了一篇日志 ,才恍然大悟:原来在AS3.0类库中最高层类(Top Level classes,所有Top Level classes的列表请看这里 )之间进行强制转换时, as 操作符是不起作用的。 I had this problem a long time, that was found yesterday in the MXNA a log, before Huangrantaiwu: the original class library in AS3.0 in the top category (Top Level classes, all the Top Level classes of the list, click here.) Between mandatory conversion, as the operator is non-functional. 比如,假如你想要将一个String 类型的字符串str 转换成Number 类型的数字num 时,可能想要这样写: For example, if you want to be a type of string str String into Number types of digital num, might want to read:
num = str as Number; num = str as Number;
这样写是没有用的,你只能通过第一种方法来达到强制转换的目的: Written like this is of no use, you only through the first method to achieve the purpose of forced conversion:
num = Number(str); num = Number (str);
特别感谢raghuonflex对此做了说明,也希望这些对学习Flex的人有些提示。 Special thanks raghuonflex have done that, I hope these people to learn Flex some tips.
注:以上技巧我只在Flex 中验证过,本人对Flash一窍不通,不保证也适用于Flash中的AS3.0 。 Note: The above skills I just verified in the Flex, I know nothing about the Flash, does not guarantee also applies to Flash in AS3.0.
本文链接: http://www.zhuoqun.net/html/y2007/654.html 转载请注明出处,谢谢。 This link: http://www.zhuoqun.net/html/y2007/654.html reprint please reference, thank you.
TrackBack引用地址: http://www.zhuoqun.net/html/y2007/654.html/trackback TrackBack used Address: http://www.zhuoqun.net/html/y2007/654.html/trackback



Flex/Flash的Actionscript文档中,除了Object(value:*)、String(value:*)、Number(value:*)等顶级类强制转换函数有说明外,根本就没有对[i]ClassName[/i](value:*)的说明。 Flex / Flash Actionscript the document, in addition to Object (value: *), String (value: *), Number (value: *), and other top category mandatory conversion function has stated, there is no right [i] ClassName [/ i] (value: *) Note. 我找了很久也没找到! I did not look for a long time to find!
to frogcjn: 对啊,那种是AS2.0中的用法,现在推荐使用as 操作符。 to frogcjn: right ah, that is AS2.0 in usage, is now recommended as operator.
这个像C#的语法. Such as C # syntax.