簡體   English   中英

actionscript-3:重構接口繼承以消除模糊引用錯誤

[英]actionscript-3: refactor interface inheritance to get rid of ambiguous reference error

想象有通過合成圖案布置的兩個接口,其中一個有一個dispose在其他方法中的方法:

interface IComponent extends ILeaf {
    ...
    function dispose() : void;
}

interface ILeaf {
    ...
}

一些實現有一些共同點(比如id ),所以還有兩個接口:

interface ICommonLeaf extends ILeaf {
    function get id() : String;
}

interface ICommonComponent extends ICommonLeaf, IComponent {
}

到現在為止還挺好。 但是還有另一個接口也有一個dispose方法:

interface ISomething {
    ...
    function dispose() : void;
}

ISomething由ICommonLeaf繼承:

interface ICommonLeaf extends ILeaf, ISomething {
    function get id() : String;
}

一旦dispose方法在其上實現了一個實例調用ICommonComponent接口,編譯器將失敗,不明確的引用錯誤,因為ISomething有一個方法稱為disposeILeaf還具有dispose方法,都生活在不同接口( IComponent, ISomething )在ICommonComponent的繼承樹中。

我想知道如何應對這種情況

  • IComponentILeafISomething無法改變。
  • 復合結構也必須適用於ICommonLeafICommonComponent
  • 實現和ICommonLeafICommonComponent必須符合ISomething類型。

這可能是一個actionscript-3特定問題。 我還沒有測試其他語言(例如java)如何處理這樣的東西。

您正在尋找鑽石問題的解決方案。 C#有一個方法,但基本上我會把方法“dispose”從你的接口中分解出來並創建一個新的“IDisposable”。

如果使用兩次相同的名稱(如“id”),則代碼中的問題看起來就像一個含糊不清的名稱。 我們開始為屬性和方法添加前綴。 想象一下,你有一個屬性“名稱”屬於兩個不同的東西。 就像“displayName”和“uniqueName”一樣。

這也有助於自動完成。 如果DisplayObject是ILayoutObject並且您鍵入displayObject.layout則會獲得所有布局相關聯。

似乎鑄造解決了模糊性,即使它遠非整潔。

class SomeComponent implements ICommonComponent {}

var c : ICommonComponent = new SomeComponent();
trace(ISomething(c).dispose()); //compiles
trace(IComponent(c).dispose()); //compiles
trace(c.dispose());    //fails

據我所知,在Actionscript中沒有簡潔的方法來處理這個問題。

我唯一能想到的就是重構你的界面以避免名字沖突,不過,這並不總是可能的。

不了解Java,但C#有辦法通過顯式接口實現來處理這個問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM