2012-11-23 40 views
0

MyCollection是一個可以有多個擴展類的通用類,它是否可以創建一個正在使用的實例的新實例?AS3 - 基類可以創建與自己的實例相同的擴展類的新實例嗎?

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; 
+0

如若ItemCollection和ViewCollectio延長MyCollection的? 新的MyCollection將是MyCollection。您需要重新解釋問題或提供更多的上下文。 –

+0

每個祖先類應該實現它自己的版本的getUnionWith方法:) –

回答

1

我認爲類似於描述here可以在您的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; 

} 
+0

這就是我一直在尋找!謝謝 – Flion