就是这种情况:Delphi的继承重叠:在创建子见父亲
3 involucrated:一个myComponent的部件,一个祖先形式和子形式:(编辑)
MyComponent的:
unit Component1;
interface
uses
System.SysUtils, System.Classes, Vcl.Dialogs;
type
TMyComponent = class(TComponent)
private
{ Private declarations }
procedure Something(i: Integer);
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
published
{ Published declarations }
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Samples', [TMyComponent]);
end;
{ TMyComponent }
constructor TMyComponent.Create(AOwner: TComponent);
var
i: integer;
begin
inherited Create(AOwner);
if AOwner.ComponentCount > 0 then
for i := 0 to AOwner.ComponentCount -1 do
Something(i);
end;
procedure TMyComponent.Something(i: Integer);
var
txt: string;
begin
txt := Format('Owner Name is %s, Owner Class is %s, ComponentCount is %d,'+
'myIndex is %d, My name is %s, my class is %s',
[Owner.Name, Owner.ClassName, Owner.ComponentCount, i, Owner.Components[i].Name,
Owner.Components[i].ClassName]);
ShowMessage('Hello '+txt);
end;
end.
祖先形态:
unit Ancestor;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Component1;
type
TmyAncestor = class(TForm)
MyComponent1: TMyComponent;
private
{ Private declarations }
public
{ Public declarations }
end;
var
myAncestor: TmyAncestor;
implementation
{$R *.dfm}
end.
子窗体:
unit TheChild;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Ancestor, Vcl.StdCtrls, Component1;
type
TmyChild = class(TmyAncestor)
edt1: TEdit;
private
{ Private declarations }
public
{ Public declarations }
end;
var
myChild: TmyChild;
implementation
{$R *.dfm}
end.
朝鲜民主主义人民共和国:
program InheritanceTest;
uses
Vcl.Forms,
Ancestor in 'Ancestor.pas' {myAncestor},
TheChild in 'TheChild.pas' {myChild};
{$R *.res}
begin
Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.CreateForm(TmyChild, myChild);
Application.Run;
end.
子窗体从祖先窗体继承myComponent的。
创建时,子窗体会触发构造函数TMyComponent.Create()
,但AOwner.ComponentCount
请参阅祖先ComponentCount
而不是子项的ComponentCount
。
消息(从myComponent.Something()
法)表明这一点:
“你好所有者姓名myAncestor,所有者类是TMyChild,ComponentCount是1,myIdex是0,我的名字是,我的课是TMyComponent”
组件在子窗体中看不到edt1
组件!
如何查看正确的ComponentCount?
A [MCVE]将有助于 –
我可以附加文件? – NizamUlMulk
请勿附加文件。制作一个[mcve]并将它编辑成问题 –