delphi實現一個類繼承抽象類別並實現介面

來源:互聯網
上載者:User

 問題描述:TSaleOutDao即要繼承抽象類別TABizDao又要實現介面TIBizDao.

解決辦法:先用抽象類別繼承TIBizDao,然後再把需要實現的介面方法聲明為抽象方法。然後再用

TSaleOutDao繼承TABizDao.

  1. //IBizDao.pas單元檔案
  2. unit IBizDao;
  3. interface
  4. type
  5.   TIBizDao=interface
  6.     function aaa:string;
  7.   end;
  8. implementation
  9. end.

 

  1. //ABizDao.pas單元檔案
  2. unit ABizDao;
  3. interface
  4. uses 
  5.   IBizDao;//引用父類單元
  6. type
  7.   {特別注意,同一個單元內的兩個類會被任務是友元類,即如果把SaleOutDao也寫在該單元中,
  8. SaleOutDao可以直接存取ABizDao中的私人變數TableName並賦值}
  9.   TABizDao=class(TInterfacedObject,TIBizDao)
  10.   private
  11.     TableName:string;
  12.   protected
  13.     
  14.   public
  15.     constructor create;
  16.     destructor destroy;override;
  17.     procedure SetTableName;virtual;abstract;
  18.     procedure SetTableNameEx(FName:string);
  19.     function GetTableNameEx:string;
  20. {//雖然是繼承自介面,但不實現介面的方法;aaa聲明為抽象方法,在子類實現}
  21.     function aaa: String;virtual;abstract;
  22.   end;
  23. implementation
  24. { ABizDao }
  25. constructor TABizDao.create;
  26. begin
  27.   Self.SetTableName;
  28. end;
  29. destructor TABizDao.destroy;
  30. begin
  31.   inherited;
  32. end;
  33. function TABizDao.GetTableNameEx: string;
  34. begin
  35.   Result:=TableName;
  36. end;
  37. procedure TABizDao.SetTableNameEx(FName: string);
  38. begin
  39.   Self.TableName:=FName;
  40. end;
  41. end.

 

  1. //SaleOutDao.pas單元檔案
  2. unit SaleOutDao;
  3. interface
  4. uses
  5.   ABizDao,IBizDao;//引用父類單元
  6. type
  7.   TSaleOutDao=class(TABizDao)
  8.   private
  9.   protected
  10.     
  11.   public
  12.     constructor create;
  13.     destructor destroy;override;
  14.     procedure SetTableName;override;
  15.     function aaa: String;override;
  16.   end;
  17. implementation
  18. {實現介面的方法}
  19. function TSaleOutDao.aaa: String;
  20. begin
  21.   Result:='aaa';
  22. end;
  23. constructor TSaleOutDao.create;
  24. begin
  25.   inherited;{加上關鍵字inherited會調用父類建構函式,如果不加的話不會執行TABizDao.create}
  26. end;
  27. destructor TSaleOutDao.destroy;
  28. begin
  29.   inherited;
  30. end;
  31. {實現抽象類別的抽象方法}
  32. procedure TSaleOutDao.SetTableName;
  33. begin
  34.   inherited;
  35.   SetTableNameEx('UserInfo');
  36. end;
  37. end.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.