如何Class
从该类的实例中获取actionscript类的实例?
在Python中,这将是x.__class__
; 在Java中x.getClass();
.
我知道有一些可怕的黑客可以做到这一点,但我正在寻找一个内置的语言设施,或者至少建立在可靠的基础上的库例程.
您可以通过基类Object的'constructor'属性获取它.即:
var myClass:Class = Object(myObj).constructor;
你有什么理由不能这样做吗?
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进行往返,但它应该运行得很好.
被接受的(当前最流行的答案)有一些缺陷.答案适用于此特定用例,但评论已将答案扩展为看似一般的解决方案.
但在某些情况下,它不是类型安全的解决方案,并不能解决所有可能的对象.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
方法允许:
Adobe为(当前和未来)有问题的领域开发内部解决方案
您不必为将来的Adobe开发更改代码
您不必开发两个(或更多)单独的动态实例化方法.
它现在可能会变慢,但在未来可能会有Adobe的改进来解决速度问题.
(例如,以前uint
在for循环中的速度要慢得多int
,因此设置了很多转换代码以便更快地使用int
.这是一个相当简单的问题需要解决,所以Adobe修复它,现在有了很多代码跳过一些不必要的箍来实现过时的目标.)
由于该getDefinitionByName
方法在所有情况下都能正常工作,因此应使用该方法开发代码,然后使Adobe解决速度问题.