Tchild=class(TThread)
private
public
end;Ta= class(tchild)
public
procedure bbb;virtual;
constructor Create;
procedure Execute;override;
end;
tb=class(ta)
private
procedure bbb;
public
end;
constructor Ta.Create;
var
iniFile: TiniFile;
begin
inherited Create(True);
FreeOnTerminate := True;
Resume;
end;
procedure ta.Execute;
begin
bbb;
end;
c:=tb.create;ta下面的子线程类tb的一个实例c创建执行,因为tb自身没有重写create 及execute函数,所以其create及execcute将执行父类中ta的这两个方法,如何在执行到ta.execcute函数
的时候,调用执行其子类tb中的实际函数bbb呢?
tb.create;
private
public
end;Ta= class(tchild)
public
procedure bbb;virtual;
constructor Create;
procedure Execute;override;
end;
tb=class(ta)
private
procedure bbb;
public
end;
constructor Ta.Create;
var
iniFile: TiniFile;
begin
inherited Create(True);
FreeOnTerminate := True;
Resume;
end;
procedure ta.Execute;
begin
bbb;
end;
c:=tb.create;ta下面的子线程类tb的一个实例c创建执行,因为tb自身没有重写create 及execute函数,所以其create及execcute将执行父类中ta的这两个方法,如何在执行到ta.execcute函数
的时候,调用执行其子类tb中的实际函数bbb呢?
tb.create;
public
procedure bbb; override; // here
end;
protected
procedure Execute;override;
procedure MyProc;virtual;abstract;
end;TChildThread = class(TParentThread)
protected
procedure MyProc;override;
end;procedure TParentThread.Execute;
begin
MyProc;
end;procedure TChildThread.MyProc;
begin
MessageBox(GetActiveWindow,'I am a TChileThread!','TChildThread',MB_OK);
end;或者也可以用下面的代码测试,可能会更加清晰
TParentThread = class(TThread)
protected
procedure Execute;override;
procedure MyProc;virtual;
end;TChildThread = class(TParentThread)
protected
procedure MyProc;override;
end;procedure TParentThread.Execute;
begin
MyProc;
end;procedure TParentThread.MyProc;
begin
MessageBox(GetActiveWindow,'I am a TParentThread!','TParentThread',MB_OK);
end;procedure TChildThread.MyProc;
begin
MessageBox(GetActiveWindow,'I am a TChileThread!','TChildThread',MB_OK);
end;
var
t: TThread;
begin
t := TChildThread.Create(true);//这里可以把TChildThread换成TParentThread试一下效果
t.FreeOnTerminate := true;
t.resume;
end;