Get a static property from class in actionscript

旧巷老猫 提交于 2019-12-24 10:49:15

问题


I have this class

package somePackage
{
    public class SomeClass
    {
        public static const FOO: SomeClass = new SomeClass("0");
        public static const BAR: SomeClass = new SomeClass("1");
        }
}

I want to be able to get those static property given it's name.

Example:

public static function getProperty(propertyName: String): SomeClass {
    //don't know what goes here
}

var property1:SomeClass = SomeClass.getProperty("FOO"); // property1 == SomeClass.FOO
var property2:SomeClass = SomeClass.getProperty("BAR"); // property2 == SomeClass.Bar

回答1:


You could use square brackets like this:

SomeClass['FOO'] 

Or if you want to put it in a method that returns a typed object:

public static function getProperty(propertyName: String):SomeClass {
    return SomeClass[propertyName]
}


来源:https://stackoverflow.com/questions/3259249/get-a-static-property-from-class-in-actionscript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!