如何从ActionScript 3中的类实例获取Class对象?

时间:2008-10-15 07:51:18

标签: flex flash actionscript-3 actionscript

如何从该类的实例中获取actionscript类Class的实例?

在Python中,这将是x.__class__;在Java中,x.getClass();

我知道存在certain terrible hacks来执行此操作,但我正在寻找内置语言工具,或者至少建立在可靠的基础上的库例程。

3 个答案:

答案 0 :(得分:69)

您可以通过基础Object类的'constructor'属性获取它。即:

var myClass:Class = Object(myObj).constructor;

答案 1 :(得分:14)

你有什么理由不能这样做吗?

var s:Sprite = new flash.display.Sprite();

var className:String = flash.utils.getQualifiedClassName( s );
var myClass:Class = flash.utils.getDefinitionByName( className ) as Class;

trace(className ); // flash.display::Sprite
trace(myClass); // [class Sprite]

var s2 = new myClass();
trace(s2); // [object Sprite]

我不知道如何通过String避免往返,但它应该运行得很好。

答案 2 :(得分:14)

接受(目前最流行的答案)有一些缺陷。答案适用于此特定用例,但评论已将答案扩展为看似一般的解决方案。

但在某些情况下它不是类型安全的解决方案,并且它不能解决所有可能的对象。不支持XML的想法已经在这里和其他地方得到了很好的解决,但类型安全的想法却没有。

假设它是由程序员创建的类对象。以下是我设置的一些测试(这是严格模式,但是本地测试)。请注意int测试结果:

var sprite:Sprite = new Sprite();
var xml:XML = new XML();
var testInt:int = 2;
var testClass:TestClass = new TestClass();
var testAnon:Object = {};

trace("classname 1 = " + getQualifiedClassName(sprite));
trace("myclass 1 = " + getDefinitionByName(getQualifiedClassName(sprite)));
trace("constructor a 1 = " + Object(sprite).constructor);
trace("constructor a 1 = " + (Object(sprite).constructor as Class));
trace("constructor b 1 = " + sprite["constructor"]);
trace("constructor b 1 = " + (sprite["constructor"] as Class));
trace("...");
trace("classname 2 = " + getQualifiedClassName(xml));
trace("myclass 2 = " + getDefinitionByName(getQualifiedClassName(xml)));
trace("constructor a 2 = " + Object(xml).constructor);
trace("constructor a 2 = " + (Object(xml).constructor as Class));
trace("constructor b 2 = " + xml["constructor"]);
trace("constructor b 2 = " + (xml["constructor"] as Class));
trace("...");
trace("classname 3 = " + getQualifiedClassName(testInt));
trace("myclass 3 = " + getDefinitionByName(getQualifiedClassName(testInt)));
trace("constructor a 3 = " + Object(testInt).constructor);
trace("constructor a 3 = " + (Object(testInt).constructor as Class));
trace("constructor b 3 = " + testInt["constructor"]);
trace("constructor b 3 = " + (testInt["constructor"] as Class));
trace("...");
trace("classname 4 = " + getQualifiedClassName(testClass));
trace("myclass 4 = " + getDefinitionByName(getQualifiedClassName(testClass)));
trace("constructor a 4 = " + Object(testClass).constructor);
trace("constructor a 4 = " + (Object(testClass).constructor as Class));
trace("constructor b 4 = " + testClass["constructor"]);
trace("constructor b 4 = " + (testClass["constructor"] as Class));
trace("...");
trace("classname 5 = " + getQualifiedClassName(testAnon));
trace("myclass 5 = " + getDefinitionByName(getQualifiedClassName(testAnon)));
trace("constructor a 5 = " + Object(testAnon).constructor);
trace("constructor a 5 = " + (Object(testAnon).constructor as Class));
trace("constructor b 5 = " + testAnon["constructor"]);
trace("constructor b 5 = " + (testAnon["constructor"] as Class));
trace("...");

TestClass定义为:

package
{
    public class TestClass
    {
    }
}

结果:

classname 1 = flash.display::Sprite
myclass 1 = [class Sprite]
constructor a 1 = [class Sprite]
constructor a 1 = [class Sprite]
constructor b 1 = [class Sprite]
constructor b 1 = [class Sprite]
...
classname 2 = XML
myclass 2 = [class XML]
constructor a 2 = 
constructor a 2 = null
constructor b 2 = 
constructor b 2 = null
...
classname 3 = int
myclass 3 = [class int]
constructor a 3 = [class Number]
constructor a 3 = [class Number]
constructor b 3 = [class Number]
constructor b 3 = [class Number]
...
classname 4 = src::TestClass
myclass 4 = [class TestClass]
constructor a 4 = [class TestClass]
constructor a 4 = [class TestClass]
constructor b 4 = [class TestClass]
constructor b 4 = [class TestClass]
...
classname 5 = Object
myclass 5 = [class Object]
constructor a 5 = [class Object]
constructor a 5 = [class Object]
constructor b 5 = [class Object]
constructor b 5 = [class Object]
...

除了目前的任何测试之外,还有充分的理由使用getDefinitionByName而不是constructor方法。 getDefinitionByName方法允许:

  • Adob​​e为(当前和未来)有问题的领域开发内部解决方案
  • 您不必为将来的Adobe开发更改代码
  • 您不必开发两个(或更多)单独的动态实例化方法。

现在可能会变慢,但将来可能会有Adobe方面的改进来解决速度问题。

(例如,之前的uint for循环比int慢得多,因此设置了大量转换代码以使用更快的int。这是一个相当简单的问题需要解决,所以Adobe修复了它,现在有很多代码会跳过一些不必要的箍来实现过时的目标。)

由于getDefinitionByName方法在所有情况下都能正常工作,因此应使用该方法开发代码,然后努力让Adobe解决速度问题。