开发者

Namespace and units

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 s开发者_JAVA百科olution 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜