[英]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
有一个方法称为dispose
和ILeaf
还具有dispose
方法,都生活在不同接口( IComponent, ISomething
)在ICommonComponent的继承树中。
我想知道如何应对这种情况
IComponent
, ILeaf
和ISomething
无法改变。 ICommonLeaf
和ICommonComponent
ICommonLeaf
和ICommonComponent
必须符合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.