[英]actionscript-3: refactor interface inheritance to get rid of ambiguous reference error
imagine there are two interfaces arranged via composite pattern, one of them has a dispose
method among other methods: 想象有通过合成图案布置的两个接口,其中一个有一个
dispose
在其他方法中的方法:
interface IComponent extends ILeaf {
...
function dispose() : void;
}
interface ILeaf {
...
}
some implementations have some more things in common (say an id
) so there are two more interfaces: 一些实现有一些共同点(比如
id
),所以还有两个接口:
interface ICommonLeaf extends ILeaf {
function get id() : String;
}
interface ICommonComponent extends ICommonLeaf, IComponent {
}
so far so good. 到现在为止还挺好。 but there is another interface which also has a
dispose
method: 但是还有另一个接口也有一个
dispose
方法:
interface ISomething {
...
function dispose() : void;
}
and ISomething
is inherited by ICommonLeaf: 和
ISomething
由ICommonLeaf继承:
interface ICommonLeaf extends ILeaf, ISomething {
function get id() : String;
}
As soon as the dispose
method is invoked on an instance which implements the ICommonComponent
interface, the compiler fails with an ambiguous reference error because ISomething
has a method called dispose
and ILeaf
also has a dispose
method, both living in different interfaces ( IComponent, ISomething
) within the inheritace tree of ICommonComponent. 一旦
dispose
方法在其上实现了一个实例调用ICommonComponent
接口,编译器将失败,不明确的引用错误,因为ISomething
有一个方法称为dispose
和ILeaf
还具有dispose
方法,都生活在不同接口( IComponent, ISomething
)在ICommonComponent的继承树中。
I wonder how to deal with the situation if 我想知道如何应对这种情况
IComponent
, the ILeaf
and the ISomething
can't change. IComponent
, ILeaf
和ISomething
无法改变。 ICommonLeaf
& ICommonComponent
ICommonLeaf
和ICommonComponent
ICommonLeaf
& ICommonComponent
must conform to the ISomething
type. ICommonLeaf
和ICommonComponent
必须符合ISomething
类型。 this might be an actionscript-3 specific issue. 这可能是一个actionscript-3特定问题。 i haven't tested how other languages (for instance java) handle stuff like this.
我还没有测试其他语言(例如java)如何处理这样的东西。
You are searching for a solution to the Diamond Problem. 您正在寻找钻石问题的解决方案。 C# has an approach to this but basically I would factor the method "dispose" out of your interfaces and create a new "IDisposable".
C#有一个方法,但基本上我会把方法“dispose”从你的接口中分解出来并创建一个新的“IDisposable”。
If the same name like "id" is used twice, it looks like a problem in your code with an ambiguous name. 如果使用两次相同的名称(如“id”),则代码中的问题看起来就像一个含糊不清的名称。 We started to add prefixes to properties and methods.
我们开始为属性和方法添加前缀。 Imagine you have a property "name" that belongs to two different things.
想象一下,你有一个属性“名称”属于两个不同的东西。 Like the "displayName" and the "uniqueName".
就像“displayName”和“uniqueName”一样。
This also helps with auto completion. 这也有助于自动完成。 If a DisplayObject is an ILayoutObject and yout type
displayObject.layout
you get everything layout releated. 如果DisplayObject是ILayoutObject并且您键入
displayObject.layout
则会获得所有布局相关联。
It seems casting solves the ambiguity even though it's far from neat. 似乎铸造解决了模糊性,即使它远非整洁。
class SomeComponent implements ICommonComponent {}
var c : ICommonComponent = new SomeComponent();
trace(ISomething(c).dispose()); //compiles
trace(IComponent(c).dispose()); //compiles
trace(c.dispose()); //fails
As far as I'm aware, there's no neat way to deal with this problem in Actionscript. 据我所知,在Actionscript中没有简洁的方法来处理这个问题。
The only thing I can think of is refactoring your interfaces to avoid name clashes, which, admitedly, it's not always possible. 我唯一能想到的就是重构你的界面以避免名字冲突,不过,这并不总是可能的。
Don't know about Java, but C# has a way to handle this through explicit interface implementation . 不了解Java,但C#有办法通过显式接口实现来处理这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.