你真的不能按照你说的去做。如果尚未构造实例,您可能无法访问实例上的非静态方法。至于 Jonatan 关于调用 super 的构造函数的评论,如果您不在构造函数主体中调用 super(),它会自动出现在方法的顶部,这是隐式完成的。当您在面向对象的语言中构造一个对象时,您正在为该类的所有成员分配内存。
如果你说:
var myVar:MyObject;
myVar.doSomething(); //this line creates a null pointer exception because myVar is null
相反,如果您要说:
var myVar:MyObject = MyObject.createInstance(); // assuming createInstance is a static method that returns an instance of MyObject
myVar.doSomething(); //assuming createInstance() didn't return null then this works
But in this second case you can't reference "this" keyword from within the static method createInstance().
If you show a full example that refutes what I'm stating then I'll run it and delete my post, but I'm pretty sure I'm right here.