Differences between class declarations

。_饼干妹妹 提交于 2019-11-30 14:51:31

问题


There are many ways to declare a new class type:

  1. TMyClass1 = TObject;
  2. TMyClass2 = type TObject;
  3. TMyClass3 = class end;
  4. TMyClass4 = class(TObject);
  5. TMyClass5 = class(TObject) end;

It's my understanding that class 3, 4 and 5 are descendants of TObject, but it's not clear how 1 and 2 differ, and what the differences between 3,4 and 5 are.

Are there any differences?


回答1:


  • TMyClass1 is just an alias - a different name for TObject
  • TMyClass2 is a strongly typed alias for TObject (we call them "type'd types"); it's very unusual to use this with classes, though, normally you'd use this with e.g. Pointer to create a handle type or something (see e.g. how this is used in Windows.pas).
  • TMyClass3 is a class, implicitly descending from TObject, with no new members.
  • TMyClass4 is a class, explicitly descending from TObject, with no new members, using the concise syntax. More normally, this is used for marker classes, where the uniqueness of the class itself is the interesting thing - often used for Exception descendants
  • TMyClass5 is a class, explicitly descending from TObject, with no new members. The TObject in the declaration is redundant, but it doesn't harm anything to make it explicit.


来源:https://stackoverflow.com/questions/5086439/differences-between-class-declarations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!