就是這種情況: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]並將它編輯成問題 –