【发布时间】:2012-02-21 15:57:08
【问题描述】:
假设我们有接口I、对象O和一个容器sl:
type
rootInterface = Interface
end;
childOfRootInterface = interface(rootInterface)
end;
ObjectImplementsRootInterface = class(TObject, I)
end;
ObjectImplementsChildOfRootInterface = class(TOtherObject, I2);
end;
var
aStringList: TStringList;
假设sl已初始化。
如果我们这样做
aStringList.Items.AddObject('root', ObjectImplementsRootInterface );
那我们做不到
ObjectImplementsRootInterface := I(aStringList.Items.Objects([0]));
因为 Delphi 禁止这样做。如此尝试:
ObjectImplementsRootInterface := TObject(aStringList.Items.Objects([0]));
但是,我们插入了一个childOfRootInterface,我们想要返回一个childOfRootInterface,所以我们这样做了
ObjectImplementsChildOfRootInterface := childOfRootInterface (sl.Items.Objects([0]));
但是ObjectImplementsChildOfRootInterface和ObjectImplementsRootInterface有不同的supers!
虽然它们支持相同的界面,但我们无能为力。
在java中你可以传递对象,只要它们支持接口,在java中是可能的,所以现在经过所有这些规划;我可以在 Delphi 中做什么?
更新:更改了名称,但是该示例可能会克服我并获胜,找不到左右。
【问题讨论】:
标签: delphi inheritance object interface