What is the difference between types defined in the implementation as compared to the interface section of a unit?
Almost all of the Delphi code I have read has all the class type definitions i开发者_运维技巧n the units interface section, but I have seen occasional use of type definitions within the implementation section.
What exactly is the difference between these, and why would I use this?
It's pretty simple: types defined in implementation only are only visible within the implementation, so they cannot be used as types of arguments or return values in the interface. So, position your type definitions (like anything else;-) based on whether those types are only an implementation detail, or something you want to make visible externally, i.e., through the interface!
Scope. Interface declarations are public and availabe to other units when that unit is include in the Uses clause. Implementation declarations are private and only available within that specific Unit.
There is a general difference between code changes in interface and the code changes implementation during compilation. If you add a class to or change an existing class in the interface section then every unit that references the changed unit will need to be recompiled. However a change in the implementation section (a new subclass or code changes) will only required the recompilation of that unit and the IDE will link the previously compiled DCU plus the new one together to create the EXE file.
Overall the major benefit, is that it allows you to design you code to hide implementation details - define the parent class in the interface and any subclasses in the implementation. Or define classes in the implementation if they are solely needed to implement the behavious of a class/method available in the interface section.
精彩评论