将类作为构造函数的参数传递

时间:2011-10-26 14:23:13

标签: actionscript-3 class constructor arguments

我正在尝试将对象类作为构造函数参数传递。我有这样的事情:

package myclass {

import flash.display.MovieClip;
import flash.display.BitmapData;
import flash.display.Bitmap;

public class Example extends MovieClip {

    public var  _stageImg:Bitmap;
    public var  _stageData:BitmapData;
    public var  _stageClass:Class;

    private var _stage:Object;

    public function Example(stageClass:Class) {
        _stageClass = stageClass;

    }

    public function createStage():Object {
        _stageData      = new _stageClass(0,0);
        _stageImg       = new Bitmap(_stageData);
        _stage          = addChild(_stageImg);

        return _stage;
    }

}
}

现在,当我尝试创建我的示例时:

    import myclass.Example;
    var example:Example = new Example(myObjClass);

我得到消息,我没有传递任何变量(错误#1063)。这是为什么?这有什么问题?

1 个答案:

答案 0 :(得分:3)

您正在传递您的类的实例,而不是类本身的定义。

在ActionScript中它有点笨拙,但你可以得到这样一个实例的类定义:

//first get the fully qualified classname, i.e. including the package
var className:String = getQualifiedClassName(myObjInstance);
//then get the class definition for that name
var Klass:Class = getDefinitionByName(className) as Class;
//and finally pass it to your constructor
var example:Example = new Example(Klass);

(注意:我将变量命名为'Klass',因为'Class'是保留关键字)