Difference Between Class Reference And Class Instance
Given that both of these calls to getQualifiedClassName return the same thing (mx:Label), how would one go about programatically differentiating between an instance of a class and a reference to the class...
var lab1:Label=new Label();
var lab2:Class=Label;
var qcn1:String=getQual开发者_高级运维ifiedClassName(lab1);
var qcn2:String=getQualifiedClassName(lab2);
In other words, how could I know that lab1 is of type Label, while lab2 is type "Class".
typeof() simply returns "object" for both... getQualifiedClassName returns "mx.controls::Label" for both...
What do I use?
The is
operator:
>>> lab1 is Label
true
>>> lab1 is Class
false
>>> lab2 is Label
false
>>> lab2 is Class
true
Also (although this should be trivially true) lab2 == Label
.
trace( Sprite is Class ); // true
trace( new Sprite() is Class ); // false
To test things in more details in the future, you can always use
import flash.utils.describeType;
trace(describeType(new Label()));
trace(describeType(Label));
This will give you 2 XML
outputs. Here you will be able to clearly see the difference.
I have provided 2 here for you for the Point
class, since point is a relatively simple class compared to DisplayObjects
and any class that extends off them.
Instance.
<type name="flash.geom::Point" base="Object" isDynamic="false" isFinal="false" isStatic="false">
<extendsClass type="Object"/>
<constructor>
<parameter index="1" type="Number" optional="true"/>
<parameter index="2" type="Number" optional="true"/>
</constructor>
<variable name="x" type="Number"/>
<variable name="y" type="Number"/>
<accessor name="length" access="readonly" type="Number" declaredBy="flash.geom::Point"/>
<method name="clone" declaredBy="flash.geom::Point" returnType="flash.geom::Point"/>
<method name="add" declaredBy="flash.geom::Point" returnType="flash.geom::Point">
<parameter index="1" type="flash.geom::Point" optional="false"/>
</method>
<method name="equals" declaredBy="flash.geom::Point" returnType="Boolean">
<parameter index="1" type="flash.geom::Point" optional="false"/>
</method>
<method name="subtract" declaredBy="flash.geom::Point" returnType="flash.geom::Point">
<parameter index="1" type="flash.geom::Point" optional="false"/>
</method>
<method name="normalize" declaredBy="flash.geom::Point" returnType="void">
<parameter index="1" type="Number" optional="false"/>
</method>
<method name="offset" declaredBy="flash.geom::Point" returnType="void">
<parameter index="1" type="Number" optional="false"/>
<parameter index="2" type="Number" optional="false"/>
</method>
<method name="toString" declaredBy="flash.geom::Point" returnType="String"/>
</type>
Class
<type name="flash.geom::Point" base="Class" isDynamic="true" isFinal="true" isStatic="true">
<extendsClass type="Class"/>
<extendsClass type="Object"/>
<accessor name="prototype" access="readonly" type="*" declaredBy="Class"/>
<method name="polar" declaredBy="flash.geom::Point" returnType="flash.geom::Point">
<parameter index="1" type="Number" optional="false"/>
<parameter index="2" type="Number" optional="false"/>
</method>
<method name="interpolate" declaredBy="flash.geom::Point" returnType="flash.geom::Point">
<parameter index="1" type="flash.geom::Point" optional="false"/>
<parameter index="2" type="flash.geom::Point" optional="false"/>
<parameter index="3" type="Number" optional="false"/>
</method>
<method name="distance" declaredBy="flash.geom::Point" returnType="Number">
<parameter index="1" type="flash.geom::Point" optional="false"/>
<parameter index="2" type="flash.geom::Point" optional="false"/>
</method>
<factory type="flash.geom::Point">
<extendsClass type="Object"/>
<constructor>
<parameter index="1" type="Number" optional="true"/>
<parameter index="2" type="Number" optional="true"/>
</constructor>
<variable name="x" type="Number"/>
<variable name="y" type="Number"/>
<accessor name="length" access="readonly" type="Number" declaredBy="flash.geom::Point"/>
<method name="clone" declaredBy="flash.geom::Point" returnType="flash.geom::Point"/>
<method name="add" declaredBy="flash.geom::Point" returnType="flash.geom::Point">
<parameter index="1" type="flash.geom::Point" optional="false"/>
</method>
<method name="equals" declaredBy="flash.geom::Point" returnType="Boolean">
<parameter index="1" type="flash.geom::Point" optional="false"/>
</method>
<method name="subtract" declaredBy="flash.geom::Point" returnType="flash.geom::Point">
<parameter index="1" type="flash.geom::Point" optional="false"/>
</method>
<method name="normalize" declaredBy="flash.geom::Point" returnType="void">
<parameter index="1" type="Number" optional="false"/>
</method>
<method name="offset" declaredBy="flash.geom::Point" returnType="void">
<parameter index="1" type="Number" optional="false"/>
<parameter index="2" type="Number" optional="false"/>
</method>
<method name="toString" declaredBy="flash.geom::Point" returnType="String"/>
</factory>
</type>
The Main differences being base="Object"
and base="Class"
and also the Class one has a <factory />
node. There are obviosuly more differences, but those are usually the parts I concentrate on the most.
精彩评论