0

MyCollection 是一个可能有许多扩展类的通用类,是否可以为正在使用的任何类型的实例创建一个新实例?

public class MyCollection extends Array {
    public function getUnionWith(someCollection:MyCollection):MyCollection {
        //can I create and return a new instance of the same class as this instance here? 
        //(so in this example: a new ViewCollection or ItemCollection)
    }
}
public class ItemCollection extends MyCollection { }
public class ViewCollection extends MyCollection { }

...

//so that this will work:
var viewsOccuringInBoth:ViewCollection = viewCollection1.getUnionWith(viewCollection2) as ViewCollection;
//but this will work too!
var itemsOccuringInBoth:ItemCollection = itemCollection1.getUnionWith(itemCollection2) as ItemCollection;
4

1 回答 1

1

我认为类似于这里描述的可以用在你的 getUnionWith 方法中:

public function getUnionWith(someCollection:MyCollection):MyCollection
{
    var clone:MyCollection = new (this as Object).constructor();//MUST NOT have any required arguments in constructor otherwise it will throw error

    //here do merging

    return clone;

}
于 2012-11-23T11:47:00.680 回答