Sign in Join
Wiki copytemporary

extended class syntax: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: extended class syntax

OldNewLine
1+### Static fields, static properties, regular fields and regular properties
2+
3+``` pascal
4+type
5+ TSomeClass = class
6+ private
7+ type
8+ TSomeType = type integer; // an internal type
9+ class var
10+ FSomeClassVar: TSomeType; // class variable belongs to a class, not an instance
11+ var
12+ FSomeIntanceVar: TSomeType; // instance variable belongs to instance. it is a usual field
13+ class procedure SetSomeClassVar(const AValue: TSomeType); static;
14+ public
15+ class property SomeProperty: TSomeType read FSomeClassVar write SetSomeClassVar; // class property - belongs to a class
16+ property SomeInstanceProp: TSomeType read FSomeIntanceVar;
17+ end;
18+
19+{ TSomeClass }
20+
21+class procedure TSomeClass.SetSomeClassVar(const AValue: TSomeType);
22+begin
23+ FSomeClassVar := AValue;
24+end;
25+
26+var
27+ SomeClass: TSomeClass;
28+begin
29+ SomeClass.SomeProperty := 1;
30+ WriteLn(SomeClass.SomeProperty);
31+end.
32+```
33+
34+### Class constants
35+
36+``` pascal
37+type
38+ TSomeClass = class
39+ strict private
40+ const
41+ PrivateConst = 1;
42+ type
43+ PrivateType = type Integer;
44+ var
45+ FPrivateField: PrivateType;
46+ public
47+ procedure DoSomething(Value: PrivateType = PrivateConst);
48+ function ReturnSomething: PrivateType;
49+ property SomeProp: PrivateType read FPrivateField write FPrivateField default PrivateConst;
50+ end;
51+
52+ procedure TSomeClass.DoSomething(Value: PrivateType = PrivateConst);
53+ begin
54+ end;
55+
56+ function TSomeClass.ReturnSomething: PrivateType;
57+ begin
58+ Result := PrivateConst;
59+ end;
60+
61+begin
62+end.
63+```
64+
65+### Nested classes
66+
67+``` pascal
68+type
69+ TSomeClass = class
70+ strict private
71+ type
72+ TPrivateNestedClass = class
73+ public
74+ procedure DoSomething;
75+ end;
76+ public
77+ type
78+ TNestedClass = class
79+ public
80+ procedure DoSomething;
81+ end;
82+ class procedure Test; virtual;
83+ end;
84+
85+ TDescendant = class(TSomeClass)
86+ public
87+ class procedure Test; override;
88+ end;
89+
90+procedure TSomeClass.TPrivateNestedClass.DoSomething;
91+begin
92+ WriteLn('TSomeClass.TPrivateNestedClass.DoSomething: ok');
93+end;
94+
95+procedure TSomeClass.TNestedClass.DoSomething;
96+begin
97+ WriteLn('TSomeClass.TNestedClass.DoSomething: ok');
98+end;
99+
100+class procedure TSomeClass.Test;
101+var
102+ P: TPrivateNestedClass;
103+ N: TNestedClass;
104+begin
105+ P := TPrivateNestedClass.Create;
106+ P.DoSomething;
107+ P.Free;
108+ N := TNestedClass.Create;
109+ N.DoSomething;
110+ N.Free;
111+end;
112+
113+class procedure TDescendant.Test;
114+var
115+ N: TNestedClass;
116+begin
117+ N := TNestedClass.Create;
118+ N.DoSomething;
119+ N.Free;
120+end;
121+
122+begin
123+ TSomeClass.Test;
124+ TDescendant.Test;
125+end.
126+```
127+
128+---
129+*From the Free Pascal wiki, page "extended class syntax", as it stood on 2020-02-14 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history