代码之家  ›  专栏  ›  技术社区  ›  maxmc

actionscript-3:重构接口继承以消除不明确的引用错误

  •  1
  • maxmc  · 技术社区  · 16 年前

    假设有两个界面通过复合模式排列,其中一个有一个 dispose 方法和其他方法:

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

    一些实现有更多的共同点(比如 id )所以还有两个接口:

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

    到现在为止,一直都还不错。但是还有一个接口也有一个 处置

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

    ISomething 由ICommonLeaf继承:

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

    一旦 处置 ICommonComponent 接口时,编译器失败并出现不明确的引用错误,因为 等长的 有一个名为 处置 ILeaf 也有一个 处置 IComponent, ISomething )在ICommonComponent的inheritace树中。

    • 这个 IComponent 伊莱夫 以及 等长的 不能改变。
    • 复合结构也必须适用于 ICommonLeaf
    • 实现和 ICommonLeaf公司 & ICommon组件 必须符合 等长的 类型。

    这可能是actionscript-3特定的问题。我还没有测试过其他语言(比如java)如何处理这样的东西。

    3 回复  |  直到 16 年前
        1
  •  3
  •   Joa Ebert Emran    16 年前

    你在寻找解决钻石问题的办法。C#对此有一种方法,但基本上我会将方法“dispose”从接口中排除,并创建一个新的“IDisposable”。

    如果像“id”这样的同一个名称被使用了两次,那么在代码中使用一个模棱两可的名称似乎是一个问题。我们开始给属性和方法添加前缀。假设您有一个属于两个不同事物的属性“名称”。比如“displayName”和“uniqueName”。

    这也有助于自动完成。如果DisplayObject是ILayoutObject和yout类型 displayObject.layout

        2
  •  1
  •   maxmc    16 年前

    这似乎解决了模棱两可的问题,尽管它还远远不够整洁。

    class SomeComponent implements ICommonComponent {}
    
    var c : ICommonComponent = new SomeComponent();
    trace(ISomething(c).dispose()); //compiles
    trace(IComponent(c).dispose()); //compiles
    trace(c.dispose());    //fails
    
        3
  •  0
  •   Juan Pablo Califano    16 年前

    据我所知,在Actionscript中没有一种简洁的方法来处理这个问题。

    我唯一能想到的就是重构接口以避免名称冲突,令人钦佩的是,这并不总是可能的。

    explicit interface implementation .