1.17.检测对象数据类型

1.17.1.问题
我想检测下传入到方法的对象是什么类型。
1.17.2.解决办法
使用is操作符检测对象类型或者是父类对象的type属性。
1.17.3.讨论
要检测一个对象的类型,ActionScript提供了is操作符,检测对象类型并返回true或false。如果对象与测试目标一致或是其子类则返回true,比如,因为Canvas对象继承自UIComponent,is操作符返回true。如果IComponent测试它为Canvas类型则返回false。因为UIComponent并不是继承自Canvas。看下面的代码:
+展开
-ActionScript
public function TypeTest() {
var uiComponent:UIComponent = new UIComponent();
var canvas:Canvas = new Canvas();
trace("uiComponent is UIComponent "+(uiComponent isUIComponent));
trace("uiComponent is Canvas "+(uiComponent isCanvas));
trace("canvas is UIComponent " +(canvas is UIComponent));
}
输出一下内容:
uiComponent is UIComponent true
uiComponent is Canvas false
canvas is UIComponent true
类型检测最常用到的地方是当组件抛出一个事件时。在事件处理函数中检测是什么对象发出动作。
+展开
-ActionScript
private function eventListener(mouseEvent:MouseEvent):void {
if (mouseEvent.target is Button) {
/* handle button specific actions */
}
else if (mouseEvent.target is ComboBox) {
/* handle combobox specific things */
}
else {
/* handle all other cases */
}
}

加支付宝好友偷能量挖...


评论(0)网络
阅读(72)喜欢(0)flash/flex/fcs/AIR