[fpc-pascal]OO problem

Yet Another Geek yetanothergeek at yahoo.com
Sun Sep 29 12:36:17 CEST 2002


>> In java you can do this...
>> Super.start();
>> Can this be done in FPC ?

> Do you mean heritage? Like this:
> procedure B.start;
> begin
> // inherited A; { <- This is wrong! }

inherited start; { <- Should be this.}

> end;



You can also typecast the implicit "self" parameter
to inherit from any class in the family tree:

program inherit;
{$MODE OBJFPC}

// Grandparent:
type tObjA = class
  procedure start;
end;

// Parent:
type tObjB = class (tObjA)
  procedure start; 
end;

// Child:
type tObjC = class (tObjB)
  procedure start;
end;

// tObjA implements a "start" method...
procedure tObjA.start;
begin
  WriteLn('Hello');
end;

// tObjB overrides the parent's method...
procedure tObjB.start;
begin
  WriteLn('World');
end;

// C can inherit from A, B, or both...
procedure tObjC.start;
begin
  tObjA(self).start; // cast to grandparent
  tObjB(self).start;  // cast to parent
end;


// Show me!
var 
  MyC:tObjC;
begin
  MyC:=tObjC.Create;
  MyC.start;
  MyC.Free;
end.



__________________________________________________
Do you Yahoo!?
New DSL Internet Access from SBC & Yahoo!
http://sbc.yahoo.com




More information about the fpc-pascal mailing list