如何隐藏published的属性

先看一下Delphi帮助中对Property的Override的说明:
A property declaration that doesn't specify a type is called a property override. Property overrides allow you to change a property's inherited visibility or specifiers. The simplest override consists only of the reserved word property followed by an inherited property identifier; this form is used to change a property's visibility. For example, if an ancestor class declares a property as protected, a derived class can redeclare it in a public or published section of the class. Property overrides can include read, write, stored, default
, and nodefault directives; any such directive overrides the corresponding inherited directive. An override can replace an inherited access specifier, add a missing specifier, or increase a property's visibility, but it cannot remove an access specifier or decrease a property's visibility. An override can include an implements directive, which adds to the list of implemented interfaces without removing inherited ones.

The following declarations illustrate the use of property overrides.

type
TAncestor = class
...
protected
property Size: Integer read FSize;
property Text: string read GetText write SetText;
property Color: TColor read FColor write SetColor stored False;
...
end;
type
TDerived = class(TAncestor)
...
protected
property Size write SetSize;
published

property Text;
property Color stored True default clBlue;
...
end;

The override of Size adds a write specifier to allow the property to be modified. The overrides of Text and Color change the visibility of the properties from protected to published. The property override of Color also specifies that the property should be filed if its value isn't clBlue.

A redeclaration of a property that includes a type identifier hides the inherited property rather than overriding it. This means that a new property is created with the same name as the inherited one. Any property declaration that specifies a type must be a complete declaration, and must therefore include at least one access specifier.

Whether a property is hidden or overridden in a derived class, property look-up is always static. That is, the declared (compile-time) type of the variable used to identify an object determines the interpretation of its property identifiers. Hence, after the following code executes, reading or assigning a value to MyObject.Value invokes Method1 or Method2, even though MyObject holds an instance of
TDescendant. But you can cast MyObject to TDescendant to access the descendant class's properties and their access specifiers.

type
TAncestor = class
...
property Value: Integer read Method1 write Method2;
end;
TDescendant = class(TAncestor)
...
property Value: Integer read Method3 write Method4;
end;
var MyObject: TAncestor;
...
MyObject := TDescendant.Create;


上面的意思是说,属性是可以继承的,如果不指定类型,则表示继承属性,可修改属性的读写方法和可见性,可见性的修改只能增大;如果指定了类型,则表示定义新的属性,父类的同名属性被覆盖了;
要访问父类的属性,使用Inherited关键字,如 Inherited Caption := 'test';
也就说,一个属性一旦被Published,则Object Inspector中就无法隐藏了,但是,Object Inspector默认是不显示只读属性的,利用这一特性,我们可以将不想显示的属性改为只读:如下所示:
THideProp = class(TScrollBox)
private
function GetColor: TColor;
protected
public
published
property Color: TColor read GetColor;
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('Samples', [THideProp]);
end;

function THideProp.GetColor: TColor;
begin
result := Inherited Color;
end;

上面的例子中,Color属性变为只读属性,在Object Inspector的默认选项中就不会出现了。
注意:新的属性也必须声明为Published,声明在Public中的话在Object Inspector中仍会出现

原来已published的属性;



更新: 在Delphi XE的IDE中,只读属性默认是显示的,只是颜色是灰的,可以设置为不显示。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值