flex如何通过类名称实例化对象

本文介绍如何在Flex中获取对象所属的类信息,并提供了两种动态实例化类的方法。一种是通过getDefinitionByName获取完全限定名,另一种是在代码中直接指定类名。此外还介绍了确保动态实例化的类已编译到SWF文件中的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[b]Getting the class from an object[/b]
Class(getDefinitionByName(getQualifiedClassName(obj)));

[b]Getting Class info of an Object in Flex[/b]
Frequently we may come across one requirement when we will be having one object and we want to know which class this object belongs to. In java, its as simple as object. getClass();
Flex also provides us with a class, mx.utils.ObjectUtil, which we can use for this purpose.
ObjectUtil.getClassInfo(object)
will return information about the class, and properties of the class, for the specified Object.
• name: String containing the name of the class;
• Properties: Sorted list of the property names of the specified object.
These are the properties we can use for that.
Alert.show(ObjectUtil.getClassInfo(faultEvent.target).name);
It’s often useful to know which class had generated some fault event and we can use the above code

[b]How to instantiate Class from Class Name [/b]
One frequently asked question is how to instantiate a Class if you don't know the Class until runtime. The "new" operator simply operates on a Class. There's no reason that class has to be hard coded. It could be a variable of Class type. The trick then becomes setting the variable. Often this is done by using getDefintionByName () as below:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" applicationComplete="init()">
<mx:Script>
<![CDATA[
import flash.utils.getDefinitionByName;
import myPackage.mySubPackage.MyClass; // all classes you may want to instantiate should be imported.
import myPackage.mySubPackage.MyClass2; // all classes you may want to instantiate should be imported.

public var dummy:MyClass; // forces "MyClass" to be linked in by the complier";
public var dummy:MyClass2; // forces "MyClass2" to be linked in by the complier";

public function init():void {
var className:String;
if (true) { // really this should be a useful conditional
className = "myPackage.mySubPackage.MyClass"; //use fully qualified name
} else {
className = "myPackage.mySubPackage.MyClass2"; //use fully qualified name
}
var definition:Class = getDefinitionByName(className) as Class; // get class
var myInstance:Object = new definition(); // create new instance of the class of type MyClass
}
]]>
</mx:Script>
</mx:Application>



However this requires you to have the fully qualified name. Sometimes this isn't practical. You could thus do something simpler which requires a little more hard coding:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" applicationComplete="init()">
<mx:Script>
<![CDATA[
import flash.utils.getDefinitionByName;
import myPackage.mySubPackage.MyClass;
import myPackage.mySubPackage.MyClass2;

public function init():void {
var definition:Class;
if (true) { // really this should be a useful conditional
definition = myClass;
} else {
definition = myClass2
}
var myInstance:Object = new definition(); // create new instance of the class of type
MyClass }
]]>
</mx:Script>
</mx:Application>


Lastly, any classes that are dynamically instantiated have to have been compiled into the SWF (or retrieved from a module/RSL/etc). Simply importing a Class does not accomplish this. (Importing is simply a way so you don't have to write the package name in front of the class every time.) There are three main ways (that I know of) to link a Class into a project.

The first is the method is to use the Class somewhere in the code. Examples of this include (as above) defining a variable of the class type or assigning the Class to a variable of type Class. Another example is rschmidt's comment below that you can instantiate an array that holds the classes.

The second method is to use the extraClass property of the Frame or Mixin tag. Warning, these methods may cause linkage problems.

Lastly, you can use the compiler options (how boring).

Personally, I think I prefer rschmidt's method. It's the most economical with code (and probably other resources as well).
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值