简体   繁体   English

为什么TObject.AfterConstruction和BeforeConstruction具有公众可见性?

[英]Why do TObject.AfterConstruction and BeforeConstruction have public visibility?

Today, one very simple question came to my mind when I had to override TObject 's BeforeConstruction method: 今天,当我不得不重写TObjectBeforeConstruction方法时,我想到了一个非常简单的问题:

Why do TObject methods AfterConstruction and BeforeConstruction have public visibility? 为什么TObject方法AfterConstructionBeforeConstruction具有public可见性?

I expected them to be protected . 我希望他们受到protected What is the reason they aren't? 他们不是的原因是什么?

I can't imagine a valid purpose to call AfterConstruction or BeforeConstruction without calling the constructor or destructor of that class. 我无法想象在没有调用该类的构造函数或析构函数的情况下调用AfterConstructionBeforeConstruction的有效目的。 Do you? 你呢?

A previous question asked why some other methods are public instead of protected, and the answer was that they at some point needed to be called by utility functions that weren't attached to the class. 之前的一个问题是为什么其他一些方法是公开的而不是受保护的, 答案是它们在某些时候需要被没有附加到类的实用函数调用。

TObject.AfterConstruction is called by just such a utility function, System._AfterConstruction . TObject.AfterConstruction只由这样的实用函数System._AfterConstruction If it were (strict) protected, then that standalone function wouldn't have access to the method. 如果它(严格)受到保护,那么该独立函数将无法访问该方法。

All the methods of TObject are public.* We can probably find rationales to explain why each method is public, but at a certain point, I suspect that the underlying reason for any given method being public is that all the others are, too. TObject 所有方法都是公开的。*我们可能找到解释为什么每个方法都是公开的理由,但在某个时刻,我怀疑任何给定方法公开的根本原因是所有其他方法也是公开的。

Once Delphi was released with those methods public, any reduction in their visibility would have risked breaking existing code. 一旦Delphi与这些方法公开发布,其可见性的任何降低都有可能破坏现有代码。


* Except for GetDisposed and CheckDisposed , for some reason. *由于某些原因, GetDisposedCheckDisposed除外。 They're relatively new, compared to the bulk of TObject . TObject的大部分相比,它们相对较新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM