Delphi implements a class to inherit abstract classes and implement interfaces

Source: Internet
Author: User

Problem description: tsaleoutdao inherits the abstract class tabizdao and implements the interface tibizdao.

Solution: Use the abstract class to inherit tibizdao, and then declare the interface method to be implemented as an abstract method. And then use

Tsaleoutdao inherits tabizdao.

  1. // Ibizdao. Pas unit file
  2. Unit ibizdao;
  3. Interface
  4. Type
  5. Tibizdao = interface
  6. Function AAA: string;
  7. End;
  8. Implementation
  9. End.

 

  1. // Abizdao. Pas unit file
  2. Unit abizdao;
  3. Interface
  4. Uses
  5. Ibizdao; // reference the parent class unit
  6. Type
  7. {Note that the two classes in the same unit will be processed by the task as the youyuan class, that is, if saleoutdao is also written in the unit,
  8. Saleoutdao can directly access the private variable tablename in abizdao and assign values}
  9. Tabizdao = Class (tinterfacedobject, tibizdao)
  10. Private
  11. Tablename: string;
  12. Protected
  13. Public
  14. Constructor create;
  15. Destructor destroy; override;
  16. Procedure settablename; virtual; abstract;
  17. Procedure settablenameex (fname: string );
  18. Function gettablenameex: string;
  19. {// Methods that inherit from the interface but do not implement the interface; AAA Declaration is an abstract method, implemented in the subclass}
  20. Function AAA: string; virtual; abstract;
  21. End;
  22. Implementation
  23. {Abizdao}
  24. Constructor tabizdao. Create;
  25. Begin
  26. Self. settablename;
  27. End;
  28. Destructor tabizdao. Destroy;
  29. Begin
  30. Inherited;
  31. End;
  32. Function tabizdao. gettablenameex: string;
  33. Begin
  34. Result: = tablename;
  35. End;
  36. Procedure tabizdao. settablenameex (fname: string );
  37. Begin
  38. Self. tablename: = fname;
  39. End;
  40. End.

 

  1. // Saleoutdao. Pas unit file
  2. Unit saleoutdao;
  3. Interface
  4. Uses
  5. Abizdao, ibizdao; // reference the parent class unit
  6. Type
  7. Tsaleoutdao = Class (tabizdao)
  8. Private
  9. Protected
  10. Public
  11. Constructor create;
  12. Destructor destroy; override;
  13. Procedure settablename; override;
  14. Function AAA: string; override;
  15. End;
  16. Implementation
  17. {Method for implementing the interface}
  18. Function tsaleoutdao. AAA: string;
  19. Begin
  20. Result: = 'aaa ';
  21. End;
  22. Constructor tsaleoutdao. Create;
  23. Begin
  24. Inherited; {with the keyword "inherited", the parent class constructor is called. If this parameter is not added, tabizdao. Create} is not executed}
  25. End;
  26. Destructor tsaleoutdao. Destroy;
  27. Begin
  28. Inherited;
  29. End;
  30. {Abstract methods for implementing abstract classes}
  31. Procedure tsaleoutdao. settablename;
  32. Begin
  33. Inherited;
  34. Settablenameex ('userinfo ');
  35. End;
  36. End.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.