If all I have is an instance of an object, can I call a static method of its class? For fun, let's say I don't know what the name of the class is, only the name of the static method.
Can I do this? How do I do this?
If all I have is an instance of an object, can I call a static method of its class? For fun, let's say I don't know what the name of the class is, only the name of the static method.
Can I do this? How do I do this?
You can use the Object's constructor property to get a reference to that object's class object, you can then call the static variable from that class object
package{
import flash.display.Sprite;
public class Test extends Sprite{
public function Test(){
var variable : A = new A();
trace((variable as Object).constructor.a());
}
}
}
class A{
static function a() : String{
return "test";
}
}
There is a simpler way, but this assumes that the static functions exists.
var myclass:Class = getDefinitionByName("MyClass") as Class;
myclass["myStaticMethod"]();
It kinda surprised me that this syntax works.
Get the class reference by it's instance.
var className:string = getQualifiedClassName(object); //returns the class name
var classObj:Class = getDefinitionByName(className) as Class; //get a Class object
Also see this - How to get type of variable? and instantiate it?