admin管理员组文章数量:1134247
This is probably somewhat dumb or even explained in the docs but I couldn't find it.
There's a pattern that seems to repeat among a lot of Delphi classes which is the creation of a TCustomSomething
before the actual TSomething
. I wonder if there's any special reason for that and in case I want to create a derivation of TSomething
should I inherit from TCustomSomething
or directly from TSomething
?
I can give some examples:
- The
TStringStream
inherits fromTByteStream
that inherits fromTMemoryStream
that finally inheritsTCustomMemoryStream
. TRESTClient
directly inherits fromTCustomRESTClient
.TButton
inherits fromTCustomButtom
.
There are indeed cases I see the actual benefit from the base TCustom...
class, like in TMemo
and TRichEdit
, but I fail to understand why TRichEdit
inherits from TCustomRichEdit
instead of directly from TCustomMemo
.
TL;DR: What's the use of the TCustomFoo
classes in Delphi? When extending an existing class should I inherit from TCustomFoo
or from TFoo
directly?
This is probably somewhat dumb or even explained in the docs but I couldn't find it.
There's a pattern that seems to repeat among a lot of Delphi classes which is the creation of a TCustomSomething
before the actual TSomething
. I wonder if there's any special reason for that and in case I want to create a derivation of TSomething
should I inherit from TCustomSomething
or directly from TSomething
?
I can give some examples:
- The
TStringStream
inherits fromTByteStream
that inherits fromTMemoryStream
that finally inheritsTCustomMemoryStream
. TRESTClient
directly inherits fromTCustomRESTClient
.TButton
inherits fromTCustomButtom
.
There are indeed cases I see the actual benefit from the base TCustom...
class, like in TMemo
and TRichEdit
, but I fail to understand why TRichEdit
inherits from TCustomRichEdit
instead of directly from TCustomMemo
.
TL;DR: What's the use of the TCustomFoo
classes in Delphi? When extending an existing class should I inherit from TCustomFoo
or from TFoo
directly?
- 2 The word "Custom" in the name usually means that it provides base behaviour that descendant classes need, but they can extend from. The same applies with "Base" in the name, With any class hierarchy, you derive from a class that closest fits your needs. For example, if you wanted to have the behaviour of a TButton, but there were properties that it exposes, or behaviour that it has that you don't want, you might derive from TCustomButton instead, which is why, for example, TColorButton (in FMX) derives from TCustomButton instead of TButton - it makes no sense for it to have a Cancel property. – Dave Nottage Commented Jan 7 at 23:05
1 Answer
Reset to default 7You can inherit from either, but you'll often find that the TFoo
class adds no functionality to the TCustomFoo
class except for making protected properties public or published, and/or making public properties into published properties.
You may want your own TAraujoFoo
components to not have all those properties public/published, in which case you can inherit from TCustomFoo
instead of TFoo
, and just make public/published the properties that you want TAraujoFoo
to have.
本文标签: inheritanceWhat39s the reason for the TCustomXXX classes in Delphi class hierarchyStack Overflow
版权声明:本文标题:inheritance - What's the reason for the TCustomXXX classes in Delphi class hierarchy? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736770540a1952061.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论