嗨,我试图绕过 parent.create 有效地调用父母的 parent.create
这是一个例子
unit InheritedTest;
interface
type
IMyExample = interface
['{4B281573-99CC-4B9D-9895-F6BCD0E26F26}']
procedure Set_MyMessage(const Value: String);
function Get_MyMessage: String;
property _MyMessage: String read Get_MyMessage write Set_MyMessage;
end;
TMyExample1 = class(TInterfacedObject, IMyExample)
protected
fMyMessage: String;
procedure Set_MyMessage(const Value: String);
function Get_MyMessage: String;
published
property _MyMessage: String read Get_MyMessage write fMyMessage;
constructor Create();
end;
TMyExample2 = class( TMyExample1)
constructor Create();
end;
TMyExample3 = class( TMyExample2)
constructor Create();
constructor CreateBypassingTMyExample2Create();
end;
implementation
{ TMyExample1 }
constructor TMyExample1.Create();
begin
inherited;
fMyMessage := 'TMyExample1';
end;
function TMyExample1.Get_MyMessage: String;
begin
Result := fMyMessage;
end;
procedure TMyExample1.Set_MyMessage(const Value: String);
begin
fMyMessage := Value;
end;
{ TMyExample2 }
constructor TMyExample2.Create;
begin
inherited;
fMyMessage := fMyMessage + ' TMyExample2';
end;
{ TMyExample3 }
constructor TMyExample3.Create;
begin
inherited;
fMyMessage := fMyMessage + ' TMyExample3.1';
end;
constructor TMyExample3.CreateBypassingTMyExample2Create;
begin
//inherited TMyExample1.Create;
inherited ClassParent.Create;
fMyMessage := fMyMessage + ' TMyExample3.2';
end;
end.
如果我创建这些类中的每一个并输出消息
uses InheritedTest;
......
procedure DoCreateTests;
var
vMyIMyExample1, vMyIMyExample2, vMyIMyExample3_1, vMyIMyExample3_2 : IMyExample;
begin
vMyIMyExample1 := TMyExample1.Create;
vMyIMyExample2 := TMyExample2.Create;
vMyIMyExample3_1 := TMyExample3.Create;
vMyIMyExample3_2 := TMyExample3.CreateBypassingTMyExample2Create;
Memo1.Lines.Add( 'vMyIMyExample1._MyMessage = ' + vMyIMyExample1._MyMessage );
Memo1.Lines.Add( 'vMyIMyExample2._MyMessage = ' + vMyIMyExample2._MyMessage );
Memo1.Lines.Add( 'vMyIMyExample3_1._MyMessage = ' + vMyIMyExample3_1._MyMessage );
Memo1.Lines.Add( 'vMyIMyExample3_2._MyMessage = ' + vMyIMyExample3_2._MyMessage );
end;
目前正在输出
vMyIMyExample1._MyMessage = TMyExample1
vMyIMyExample2._MyMessage = TMyExample1 TMyExample2
vMyIMyExample3_1._MyMessage = TMyExample1 TMyExample2 TMyExample3.1
vMyIMyExample3_2._MyMessage = TMyExample3.2
我正在尝试调用 TMyExample3.CreateBypassingTMyExample2Create 来输出
vMyIMyExample3_2._MyMessage = TMyExample1 TMyExample3.2
我需要在此通话中进行哪些更改才能使其正常工作?是否可以在不更改或(添加另一个构造函数)到 TMyExample2.create 的情况下做到这一点
constructor TMyExample3.CreateBypassingTMyExample2Create;
begin
//inherited TMyExample1.Create;
inherited ClassParent.Create;
fMyMessage := fMyMessage + ' TMyExample3.2';
end;