假设有两个接口通过复合模式排列,其中一个在其他方法中有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
有一个名为{{1}的方法}和dispose
也有一个ILeaf
方法,它们都存在于ICommonComponent的继承树中的不同接口(dispose
)中。
我想知道如何处理这种情况
IComponent, ISomething
,IComponent
和ILeaf
无法更改。ISomething
& ICommonLeaf
ICommonComponent
& ICommonLeaf
必须符合ICommonComponent
类型。这可能是一个特定于actionscript-3的问题。我还没有测试其他语言(例如java)如何处理这样的东西。
答案 0 :(得分:3)
您正在寻找钻石问题的解决方案。 C#有一个方法,但基本上我会将方法“dispose”从你的接口中分解出来并创建一个新的“IDisposable”。
如果使用两次相同的名称(如“id”),则代码中的问题看起来像一个含糊不清的名称。我们开始为属性和方法添加前缀。想象一下,你有一个属性“名称”属于两个不同的东西。像“displayName”和“uniqueName”一样。
这也有助于自动完成。如果DisplayObject是ILayoutObject并且您键入displayObject.layout
,则可以获得所有布局。
答案 1 :(得分:1)
似乎铸造解决了模糊性,即使它远非整洁。
class SomeComponent implements ICommonComponent {}
var c : ICommonComponent = new SomeComponent();
trace(ISomething(c).dispose()); //compiles
trace(IComponent(c).dispose()); //compiles
trace(c.dispose()); //fails
答案 2 :(得分:0)
据我所知,在Actionscript中没有简洁的方法来处理这个问题。
我能想到的唯一一件事就是重构你的界面以避免名字冲突,不过,这并不总是可能的。
不了解Java,但C#有办法通过explicit interface implementation来处理这个问题。