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;

解决方案 »

  1.   

    tb=class(ta)
      public
      procedure bbb; override; // here
    end; 
      

  2.   

    这不是子线程与父线程的关系,而是子类与父类之间的关系,子类与父类,定义的内容同属于当前对象.但是线程与线程之前是相互独立的,互操作就需要考虑竞争问题.TParentThread = class(TThread)
      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;
      

  3.   

    测试代码:
    var
      t: TThread;
    begin
      t := TChildThread.Create(true);//这里可以把TChildThread换成TParentThread试一下效果
      t.FreeOnTerminate := true;
      t.resume;
    end;