ActionScript 3 中的強制型別轉換 (as 強轉失敗原因)
每一種程式語言都提供強制型別轉換,允許你將某一種資料型別轉換成另一種資料型別,AS3自然也不例外。但是雖然我編寫了不少Flex程式,對 AS3中的強制型別轉換還是不太清楚,以前AS中是這樣進行強制型別轉換的:假設有一個類叫做Class1,我們聲明瞭一個它的物件 c1,如果想要將它轉換成Class2型別,只要這樣寫:
Class2(c1);
在AS3中你依然可以這樣寫,但是AS3 中提供了一個新的操作符: as ,並且推薦使用as 進行強制轉換,上述的例子用 as 操作符實現就是這樣:
c1 as Class2;
使用 as 操作符有幾個好處:
1.它的效果和第一種方法是一樣的。
2.如果型別不相容無法轉換,就會返回null,而不是出錯。這樣你就可以自定義錯誤的時候該做什麼。
3.沒有執行時錯誤(Run Time Error)提示。
不過有些時候我在使用 as 的時候並不能達到強制轉換的目的,而使用第一種方法則可以。為什麼 as 操作符有時候會不好用呢?這個問題困擾了我很久,知道昨天在MXNA上發現了一篇日誌,才恍然大悟:原來在AS3.0類庫中最高層類(Top Level classes)之間進行強制轉換時, as 操作符是不起作用的。
比如,假如你想要將一個String 型別的字串 str 轉換成 Number 型別的數字 num 時,可能想要這樣寫:
num = str as Number; //這樣寫是沒有用的,你只能通過第一種方法來達到強制轉換的目的:
num = Number(str);
例如arrary轉換成vector也只能用方法一:
vector =Vector.<String> (xmlStr.split("|",12));
Top-level classes
The top level contains the ActionScript classes and global functions, many of which provide core functionality for your applications. Core classes, borrowed directly from ECMAScript, include Array, Boolean, Date, Error, Function, Math, Number, Object, String, and System.
NOTE |
The CustomActions and XMLUI classes are available only in the Flash authoring environment. |
Class |
Description |
---|---|
Accessibility |
The Accessibility class manages communication between SWF files and screen reader applications. You use the methods of this class with the global _accProps property to control accessible properties for movie clips, buttons, and text fields at runtime. See Accessibility. |
Array |
The Array class represents arrays in ActionScript and all array objects are instances of this class. The Array class contains methods and properties for working with array objects. See Array. |
AsBroadcaster |
Provides event notification and listener management capabilities that can be added to other objects. See AsBroadcaster. |
Boolean |
The Boolean class is a wrapper for Boolean (true or false) values. See Boolean.>. |
Button |
The Button class provides methods, properties, and event handlers for working with buttons. See Button.Note that the built-in Button class is different from the Button component class, associated with the version 2 component, Button. |
Camera |
The Camera class provides access to the user's camera, if one is installed. When used with Flash Media Server, your SWF file can capture, broadcast, and record images and video from a user's camera. See Camera. |
Color |
The Color class lets you set the RGB color value and color transform of movie clip instances and retrieve those values after you set them. The Color class is deprecated in Flash Player 8 in favor of the ColorTransform class. For information on color transforms, see ColorTransform (flash.geom.ColorTransform). |
ContextMenu |
The ContextMenu class lets you control the contents of the Flash Player context menu at runtime. You can associate separate ContextMenu objects with MovieClip, Button, or TextField objects by using the menu property available to those classes. You can also add custom menu items to a ContextMenu object by using the ContextMenuItem class. See ContextMenu. |
ContextMenuItem |
The ContextMenuItem class lets you create new menu items that appear in the Flash Player context menu. You add new menu items that you create with this class to the Flash Player context menu by using the ContextMenu class. See ContextMenuItem. |
CustomActions |
The CustomActions class lets you manage any custom actions that are registered with the authoring tool. See CustomActions. |
Date |
The Date class shows how dates and times are represented in ActionScript, and it supports operations for manipulating dates and times. The Date class also provides the means for obtaining the current date and time from the operating system. See Date. |
Error |
The Error class contains information about runtime errors that occur in your scripts. You typically use the throw statement to generate an error condition, which you can handle using a try..catch..finally statement. See Error. |
Function |
The Function class is the class representation of all ActionScript functions, including those native to ActionScript and those that you define. See Function. |
Key |
The Key class provides methods and properties for getting information about the keyboard and key presses. See Key. |
LoadVars |
The LoadVars class lets you transfer variables between a SWF file and a server in name-value pairs. See LoadVars. |
LocalConnection |
The LocalConnection class lets you develop SWF files that send instructions to each other without using the fscommand() method or JavaScript. See LocalConnection. |
Math |
The Math class provides convenient access to common mathematical constants and provides several common mathematical functions. All the properties and methods of the Math class are static and must be called with the syntax Math.method(parameter) or Math.constant. See Math. |
Microphone |
The Microphone class provides access to the user's microphone, if one is installed. When used with Flash Media Server, your SWF file can broadcast and record audio from a user's microphone. See Microphone. |
Mouse |
The Mouse class provides control over the mouse in a SWF file; for example, this class lets you hide or show the mouse pointer. See Mouse. |
MovieClip |
Every movie clip in a SWF file is an instance of the MovieClip class. You use the methods and properties of this class to control movie clip objects. See MovieClip. |
MovieClipLoader |
This class lets you implement listener callbacks that provide status information while SWF, JPEG, GIF, and PNG files load into movie clip instances. See MovieClipLoader. |
NetConnection |
The NetConnection class establishes a local streaming connection for playing a Flash Video (FLV) file from an HTTP address or from the local file system. See NetConnection. |
NetStream |
The NetStream class controls playback of FLV files from a local file system or HTTP address. See NetStream. |
Number |
The Number class is a wrapper for the primitive number data type. See Number. |
Object |
The Object class is at the root of the ActionScript class hierarchy; all other classes inherit its methods and properties. See Object. |
PrintJob |
The PrintJob class lets you print content from a SWF file, including content that is rendered dynamically, and multipage documents. See PrintJob. |
Selection |
The Selection class lets you set and control the text field in which the insertion point is located (the text field that has focus). See Selection. |
SharedObject |
The SharedObject class offers persistent local data storage on the client computer, similar to cookies. This class offers real-time data sharing between objects on the client's computer. See SharedObject. |
Sound |
The Sound class provides control over sounds in a SWF file. See Sound. |
Stage |
The Stage class provides information about a SWF file's dimensions, alignment, and scale mode. It also reports Stage resize events. See Stage. |
String |
The String class is a wrapper for the string primitive data type, which lets you use the methods and properties of the String object to manipulate primitive string value types. See String. |
System |
The System class provides information about Flash Player and the system on which Flash Player is running (for example, screen resolution and current system language). It also lets you show or hide the Flash Player Settings panel and modify SWF file security settings. See System. |
TextField |
The TextField class provides control over dynamic and input text fields, such as retrieving formatting information, invoking event handlers, and changing properties such as alpha or background color. See TextField. |
TextFormat |
The TextFormat class lets you apply formatting styles to characters or paragraphs in a TextField object. See TextFormat. |
TextSnapshot |
The TextSnapshot object lets you access and lay out static text inside a movie clip. See TextSnapshot. |
Video |
The Video class lets you show video objects in a SWF file. You can use this class with Flash Media Server to display live streaming video in a SWF file, or within Flash to display a Flash Video (FLV) file. See Video. |
XML |
This class contains methods and properties for working with XML objects. See XML. |
XMLNode |
The XMLNode class represents a single node in an XML document tree. It is the XML class's superclass. See XMLNode. |
XMLSocket |
The XMLSocket class lets you create a persistent socket connection between a server computer and client running Flash Player. Client sockets enable low-latency data transfer, such as that which is required for real-time chat applications. See XMLSocket. |
XMLUI |
The XMLUI object enables communication with SWF files that are used as a custom user interface for the Flash authoring tool's extensibility features (such as Behaviors, Commands, Effects, and Tools). See XMLUI. |