fork download
  1. program ideone;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. type
  6. Parent = class
  7. class function check: boolean; virtual; abstract;
  8. procedure foo;
  9. end;
  10.  
  11. Child1 = class(Parent)
  12. class function check: boolean; override;
  13. end;
  14.  
  15. Child2 = class(Parent)
  16. class function check: boolean; override;
  17. end;
  18.  
  19. procedure Parent.foo;
  20. begin
  21. check;
  22. end;
  23.  
  24. class function Child1.check: boolean;
  25. begin
  26. writeln('Child1.check');
  27. Result := True;
  28. end;
  29.  
  30. class function Child2.check: boolean;
  31. begin
  32. writeln('Child2.check');
  33. Result := True;
  34. end;
  35.  
  36. var
  37. aclass: class of Parent;
  38. begin
  39. Child1.Create.foo;
  40. Child2.Create.foo;
  41. if random < 0.5 then aclass := Child1 else aclass := Child2;
  42. aclass.check;
  43. end.
Success #stdin #stdout 0s 4432KB
stdin
Standard input is empty
stdout
Child1.check
Child2.check
Child2.check