Class Property



Class Property
Smart pascal source code
type TBase = class class function GetName: string; virtual; class procedure SetName(v: string); virtual; property Name: string read GetName write SetName; end; type TChild = class(TBase) class function GetName: string; override; class procedure SetName(v: string); override; end; type TSubChild = class(TChild) class procedure SetName(v: string); override; end; class function TBase.GetName: string; begin Result := 'Base'; end; class procedure TBase.SetName(v: string); begin WriteLn('TBase.SetName on ' + Name + ' with ' + v); end; class function TChild.GetName: string; begin Result := 'Child'; end; class procedure TChild.SetName(v: string); begin WriteLn('TChild.SetName on ' + Name + ' with ' + v); end; class procedure TSubChild.SetName(v: string); begin WriteLn('TSubChild.SetName on ' + Name + ' with ' + v); end; { main.pas } var v: class of TBase; o: TBase; Begin WriteLn('Direct'); TBase.Name := TBase.Name; TChild.Name := TChild.Name; TSubChild.Name := TSubChild.Name; WriteLn('Class Var'); v := TBase; v.Name := v.Name; v := TChild; v.Name := v.Name; v := TSubChild; v.Name := v.Name; WriteLn('Object Var'); o := TBase.Create; o.Name := o.Name; o := TChild.Create; o.Name := o.Name; o := TSubChild.Create; o.Name := o.Name; o := nil; try o.Name := o.Name; except on E: Exception do WriteLn(e.Message); end; { ### CONSOLE OUTPUTS ### Direct TBase.SetName on Base with Base TChild.SetName on Child with Child TSubChild.SetName on Child with Child Class Var TBase.SetName on Base with Base TChild.SetName on Child with Child TSubChild.SetName on Child with Child Object Var TBase.SetName on Base with Base TChild.SetName on Child with Child TSubChild.SetName on Child with Child TypeError, Cannot read property 'ClassType' of null }