actionscript-3:重构接口继承以消除不明确的引用错误
假设有两个接口通过复合模式排列,其中一个具有actionscript-3:重构接口继承以消除不明确的引用错误,actionscript-3,design-patterns,oop,Actionscript 3,Design Patterns,Oop,假设有两个接口通过复合模式排列,其中一个具有dispose方法和其他方法: interface IComponent extends ILeaf { ... function dispose() : void; } interface ILeaf { ... } 一些实现有更多的共同点(比如id),因此还有两个接口: interface ICommonLeaf extends ILeaf { function get id() : String; } int
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;
}
只要对实现ICommonComponent
接口的实例调用dispose
方法,编译器就会因引用错误而失败,因为ISomething
有一个名为dispose
的方法,ILeaf
也有一个dispose
方法,两者都位于ICommonComponent的inheritace树中的不同接口(IComponent,ISomething
)
我想知道如果发生这种情况该如何处理
、i组件
和ILeaf
不能更改ISomething
- 复合结构还必须适用于
&ICommonLeaf
ICommonComponent
- 实现和
和ICommonLeaf
必须符合ICommonComponent
类型ISomething
这可能是actionscript-3特定的问题。我还没有测试过其他语言(例如java)如何处理这样的东西。据我所知,在Actionscript中没有处理这个问题的好方法 我唯一能想到的就是重构接口以避免名称冲突,令人钦佩的是,这并不总是可能的
我不了解Java,但C#有办法解决这个问题。据我所知,在Actionscript中没有解决这个问题的好方法 我唯一能想到的就是重构接口以避免名称冲突,令人钦佩的是,这并不总是可能的
我不了解Java,但C#有办法解决这个问题。看起来casting解决了这个模糊问题,尽管它还远远不够整洁
class SomeComponent implements ICommonComponent {}
var c : ICommonComponent = new SomeComponent();
trace(ISomething(c).dispose()); //compiles
trace(IComponent(c).dispose()); //compiles
trace(c.dispose()); //fails
看起来演员阵容解决了这个模棱两可的问题,尽管它还远远不够整洁
class SomeComponent implements ICommonComponent {}
var c : ICommonComponent = new SomeComponent();
trace(ISomething(c).dispose()); //compiles
trace(IComponent(c).dispose()); //compiles
trace(c.dispose()); //fails
你正在寻找钻石问题的解决办法。C#对此有一种方法,但基本上我会将方法“dispose”从接口中分离出来,并创建一个新的“IDisposable” 如果像“id”这样的相同名称被使用了两次,那么代码中的名称不明确似乎是个问题。我们开始在属性和方法中添加前缀。假设您有一个属于两个不同事物的属性“名称”。比如“displayName”和“uniqueName”
这也有助于自动完成。如果一个DisplayObject是一个ILayoutObject,并且yout类型为
DisplayObject.layout
,则所有布局都将被关联 您正在寻找钻石问题的解决方案。C#对此有一种方法,但基本上我会将方法“dispose”从接口中分离出来,并创建一个新的“IDisposable”
如果像“id”这样的相同名称被使用了两次,那么代码中的名称不明确似乎是个问题。我们开始在属性和方法中添加前缀。假设您有一个属于两个不同事物的属性“名称”。比如“displayName”和“uniqueName”
这也有助于自动完成。如果一个DisplayObject是一个ILayoutObject,并且yout类型为
DisplayObject.layout
,则所有布局都将被关联 是的,IDisposable接口肯定能解决问题,但不幸的是,我没有访问IComponent和ISomething接口来提取dispose。是的,IDisposable接口肯定能解决问题,但不幸的是,我没有访问IComponent和ISomething接口来提取dispose。