Class type with Const keyword



Class type with Const keyword
Smart pascal source code
{ unit1.pas } type TBase = class private const cPrivate = 1; protected const cProtected = 2; public class const cPublic = 3; procedure WriteLnAll; class procedure ClassWriteLnAll; end; type TChild = class (TBase) procedure WriteLnFromChild; end; type TBase2 = class const c1 = 1; const c2 = c1+1; end; const cTOTO = 'toto'; type maclasse1 = class private const cTOTO = 'titi'; public class procedure Test; begin WriteLn(cTOTO); end; end; type maclasse2 = class private const cTOTO = 'tata'; public class procedure Test; begin WriteLn(cTOTO); end; end; type TObj = class const Value = 5; FField := Value; // TObj.Value - OK end; type TObj2 = class private const Value = 6; public FField := TObj2.Value; end; implementation { TBase } procedure TBase.WriteLnAll; begin WriteLn('From base'); WriteLn(cPrivate); WriteLn(cProtected); WriteLn(cPublic); end; class procedure TBase.ClassWriteLnAll; begin WriteLn('From class proc'); WriteLn(cPrivate); WriteLn(cProtected); WriteLn(cPublic); end; { TChild } procedure TChild.WriteLnFromChild; begin WriteLn('From child'); WriteLn(cProtected); WriteLn(cPublic); end; { main.pas } var o := TBase.Create; Begin WriteLn('From Outside'); WriteLn(TBase.cPublic); WriteLn(o.cPublic); TBase.ClassWriteLnAll; o.WriteLnAll; TChild.Create.WriteLnFromChild; { ### CONSOLE OUTPUTS ### From Outside 3 3 From class proc 1 2 3 From base 1 2 3 From child 2 3 } { TEST II } WriteLn(TBase2.c1); // 1 WriteLn(TBase2.c2); // 2 { TEST III } WriteLn(cToto); maclasse1.Test; maclasse2.Test; { ### CONSOLE OUTPUTS ### toto titi tata } { TEST IV } Writeln(New TObj.FField); // 5 Writeln(New TObj2.FField); // 6