Class Self |
Smart pascal source code
type TBase = class class procedure MyProc; class function Virt: string; virtual; end; type TChild = class(TBase) class function Virt: string; override; end; implementation class procedure TBase.MyProc; begin if Assigned(Self) then begin Writeln(Self.ClassName); Writeln(Self.Virt); end else Writeln('nil'); end; class function TBase.Virt: string; begin Result := 'base_' + Self.ClassType.ClassName; end; class function TChild.Virt: string; begin Result := 'child_' + Self.ClassType.ClassName; end; { main.pas } var o: TBase; Begin try o.MyProc; except on E: Exception do Writeln(E.Message); end; o := TBase.Create; o.MyProc; o := TChild.Create; o.MyProc; TBase.Myproc; TChild.Myproc; { ### CONSOLE OUTPUTS ### TypeError, Cannot read property 'ClassType' of null TBase base_TBase TChild child_TChild TBase base_TBase TChild child_TChild }