如何隐藏对象的受保护过程? [英] How to hide a protected procedure of an object?

查看:95
本文介绍了如何隐藏对象的受保护过程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个基类中,有一个受保护的过程.继承该类时,我想隐藏该过程以免从外部使用.我尝试从 private 甚至是 strict private 部分覆盖它,但仍然可以从外部调用它.原始类不是我的类,因此我无法更改TOriginal的定义.

In one base class, there's a protected procedure. When inheriting that class, I want to hide that procedure from being used from the outside. I tried overriding it from within the private and even strict private sections, but it can still be called from the outside. The Original class is not mine, so I can't change how TOriginal is defined.

是否可以在继承的类中隐藏此过程?以及如何?

Is it possible to hide this procedure in my inherited class? And how?

type
  TOriginal = class(TObject)
  protected
    procedure SomeProc;
  end;

  TNew = class(TOriginal)
  strict private
    procedure SomeProc; override;
  end;

推荐答案

受保护的方法已经已经从外部隐藏了. (通常;请参阅下文.)您不能降低类成员的可见性.如果基类声明该方法受保护,则该类的所有后代都可以使用该方法.

Protected methods are already hidden from the outside. (Mostly; see below.) You cannot reduce the visibility of a class member. If the base class declared the method protected, then all descendants of that class may use the method.

在Delphi中,与类在同一单元内的其他代码也可以访问该类的受保护成员,甚至包括不相关类的代码.有时这可能很有用,但通常可以解决其他设计缺陷.如果您有真正地"应该受到保护的内容,则可以使其成为 strict 受保护的,然后特殊的相同单元访问规则将不适用.

In Delp other code within the same unit as a class may access that class's protected members, even code from unrelated classes. That can be useful sometimes, but usually to work around other design deficiencies. If you have something that's "really, really" supposed to be protected, you can make it strict protected, and then the special same-unit access rule doesn't apply.

这篇关于如何隐藏对象的受保护过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆