命名空间和单位
我知道命名空间是单元的容器;但我还不太了解如何使用它。 我尝试更好地解释我想要讲述的内容。例如我有四个单元:
1) Animals // es: class TAnimals
2) Animals.Dog // es: class TDog
3) Animals.Cat // es: class TCat
4) Animals.Cat.Female // es: class TFemale
当然,这四个单元位于四个不同的文件中。 如果我制造新的单元并需要使用所有它,我写道:
uses
Animals, Animals.Dog, Animals.Cat, Animals.Cat.Female
现在,直到很少有问题不存在,但什么时候才会有很多问题?所以我想知道我可以更好地管理,制作一个唯一的单元:包含所有其他单元的 Animal(命名空间根),具有以下内容:
uses
Animals;
并且可以从 Animal 访问第二个、第三个所有其他命名空间中定义的所有其他类等级别,例如:
program Project1;
uses
Animals;
var
x: Animals;
begin
x := TAnimals.Cat.Female.Create;
try
....
finally
x.Free;
end;
end.
在互联网上搜索,也许我发现了一些使用界面的东西,但我的印象是这不是正确的解决方案,因为在我看来,对于 delphi xe2 原生的东西来说要复杂得多。很可能我错了,或者它确实引用了一些旧版本的delphi;老实说我不知道。 但我想知道我是怎么做的,当然如果可能的话。 再次非常感谢。
i have understood that a namespace is a container of units; but i haven't understood well as work with it.
I try to explain better, about what i want tell. For example i have four units:
1) Animals // es: class TAnimals
2) Animals.Dog // es: class TDog
3) Animals.Cat // es: class TCat
4) Animals.Cat.Female // es: class TFemale
Of course, this four units are in four different file.
If i make new unit and need use all it, i write:
uses
Animals, Animals.Dog, Animals.Cat, Animals.Cat.Female
Now, until are very few problem not there is, but when are very much? So i wanted to know as i can manage better, making one only unit: Animal (namespace root) that contain all other, having so something as:
uses
Animals;
And to have access from Animal to all other classes defined in all other namespace of second,third etc level, for example:
program Project1;
uses
Animals;
var
x: Animals;
begin
x := TAnimals.Cat.Female.Create;
try
....
finally
x.Free;
end;
end.
Searching in internet, maybe i have found something using interface, but i have impression that not correct solution becouse in my opinion is much complicated for something that with delphi xe2 is native. Much probably i mistake, or it do refer to some older version of delphi; sincerly i don't know.
But i wanted know as i do to do it, of course if possibile.
Thanks again very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基本上,XE2 只是将 RTL 单元重命名为带点的名称,并尝试加载在命令行上指定的带点前缀的单元。
Delphi 中早已存在带有点的单元的功能。
忘记您对名称空间的了解,Delphi XE2 不是那样的。在命名空间层次结构中移动单元意味着对其进行重命名(更改前缀),这与 Java 中的容器/文件/类具有特定名称相反,并且只有其位置表示命名空间层次结构中的位置。
Basically XE2 just renames RTL units to names with a dot in it, and to try also to load units with a dotted prefix specified on the command line.
The ability to have units with a dot in it existed already in Delphi for a long time.
Forget whatever you know about namespaces, Delphi XE2 is not like that. Moving a unit in the namespace hierachy means renaming it (changing the prefix), contrary to e.g. Java where the container/file/class has a certain name, and only its position signals the place in the namespace hierarchy.