请问在写控件时可以把继承控件中的属性修改吗? (30分)

  • 主题发起人 主题发起人 colonel
  • 开始时间 开始时间
C

colonel

Unregistered / Unconfirmed
GUEST, unregistred user!
就如我继承的label控件,我想把它的一属性caption改成TStrings,或者删除,可以吗?



PS:前问无人回只好改了,这是修改过的贴
 
控件
1stclass 4000.01
http://www.51delphi.com/delphi/soft?type=界面
 
可以改,但是只能以 reintroduce 的形式,也就是说,两个属性已经不同了。如果以 property override 的形式,那么应该是不能改类型。

详见 Object Pascal Reference:

<!> Property overrides and redeclarations
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) {注意,如果是property override则不能为property增加类型标识}
...
protected
property Size write SetSize; {增加写方法,但保存read方法}
published
property Text; {提升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;
 
继承下来的控件除了name一项不能改外,其他好象都能修改吧
 
还是不懂怎么去改...............
 
属性还是那个属性,只是属性编辑器改了[:D]

关于属性编辑器,可以看看这两个帖子:
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1723442
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1808172
 
好像不行,像savetime说的,如果一个Property在父类中是protested那在子类中将变成是published是可以的,但反过来好象不行。
你可以不用父类继承过来的,自己定义一个属性来代替。
 
我怎么试都改不来,thx1180说的改属性编辑器是可以做到的,我不会改,因为装过一些控件它就把所有的有caption这属性的编辑器都改了,不太好,我将label的caption改成TStrings,可它说Abstract Error
 
unit wwLabel;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TwwLabel = class(TLabel)
private
FCaption: TStrings;
procedure SetCaption(const Value: TStrings);
protected
{ Protected declarations }
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property Caption: TStrings read FCaption write SetCaption;
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('WangWei', [TwwLabel]);
end;

{ TwwLabel }

constructor TwwLabel.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FCaption := TStringList.Create;
end;

destructor TwwLabel.Destroy;
begin
FCaption.Free;
inherited Destroy;
end;

procedure TwwLabel.SetCaption(const Value: TStrings);
begin
FCaption.Assign(Value);
inherited Caption := Value.Text;
end;
end.
 
这样就行了:
RegisterPropertyEditor(TypeInfo(TCaption), TMyLabel, 'Caption', TCaptionProperty);
 
多人接受答案了。
 
我觉的你非要改它的属性做什么,我建议你还是继承它,自己再创建一个属性来完成你所需要的功能.
 
你可以把那个属性(CAPTION)隐蔽起来,然后再自己另建一个属性(CAPTIONS)中,然后你自己在你的类中对之做处理,这样不就行了吗?
 
后退
顶部