extended class syntax: what changed
Revision 2 to 3. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | ### Static fields, static properties, regular fields and regular properties | |
| 2 | - | ||
| 2 | + | ||
| 3 | + | ``` pascal | |
| 3 | 4 | type | |
| 4 | - | ||
| 5 | - | ||
| 6 | - | ||
| 7 | - | ||
| 8 | - | ||
| 9 | - | ||
| 10 | - | ||
| 11 | - | ||
| 12 | - | ||
| 13 | - | ||
| 14 | - | ||
| 15 | - | ||
| 16 | - | ||
| 17 | - | ||
| 18 | - | ||
| 19 | - | ||
| 20 | - | ||
| 21 | - | ||
| 22 | - | ||
| 23 | - | ||
| 24 | - | ||
| 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 | |
| 25 | 11 | var | |
| 26 | - | ||
| 27 | - | ||
| 28 | - | ||
| 29 | - | ||
| 30 | - | ||
| 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. | |
| 31 | 32 | ``` | |
| 32 | 33 | ||
| 33 | 34 | ### Class constants | |
| 34 | - | ||
| 35 | + | ||
| 36 | + | ``` pascal | |
| 35 | 37 | type | |
| 36 | - | ||
| 37 | - | ||
| 38 | - | ||
| 39 | - | ||
| 40 | - | ||
| 41 | - | ||
| 42 | - | ||
| 43 | - | ||
| 44 | - | ||
| 45 | - | ||
| 46 | - | ||
| 47 | - | ||
| 48 | - | ||
| 49 | - | ||
| 50 | - | ||
| 51 | - | ||
| 52 | - | ||
| 53 | - | ||
| 54 | - | ||
| 55 | - | ||
| 56 | - | ||
| 57 | - | ||
| 58 | - | ||
| 59 | - | ||
| 60 | - | ||
| 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. | |
| 61 | 63 | ``` | |
| 62 | 64 | ||
| 63 | 65 | ### Nested classes | |
| 64 | - | ||
| 66 | + | ||
| 67 | + | ``` pascal | |
| 65 | 68 | type | |
| 66 | - | ||
| 67 | - | ||
| 68 | - | ||
| 69 | - | ||
| 70 | - | ||
| 71 | - | ||
| 72 | - | ||
| 69 | + | TSomeClass = class | |
| 70 | + | strict private | |
| 71 | + | type | |
| 72 | + | TPrivateNestedClass = class | |
| 73 | 73 | public | |
| 74 | - | ||
| 75 | - | ||
| 76 | - | ||
| 77 | - | ||
| 78 | - | ||
| 79 | - | ||
| 74 | + | procedure DoSomething; | |
| 80 | 75 | end; | |
| 81 | - | ||
| 82 | - | ||
| 76 | + | public | |
| 77 | + | type | |
| 78 | + | TNestedClass = class | |
| 83 | 79 | public | |
| 84 | - | ||
| 80 | + | procedure DoSomething; | |
| 85 | 81 | end; | |
| 86 | - | ||
| 87 | - | ||
| 88 | - | ||
| 89 | - | ||
| 90 | - | ||
| 91 | - | ||
| 92 | - | ||
| 93 | - | ||
| 94 | - | ||
| 95 | - | ||
| 96 | - | ||
| 97 | - | ||
| 98 | - | ||
| 99 | - | ||
| 100 | - | ||
| 101 | - | ||
| 102 | - | ||
| 103 | - | ||
| 104 | - | ||
| 105 | - | ||
| 106 | - | ||
| 107 | - | ||
| 108 | - | ||
| 109 | - | ||
| 110 | - | ||
| 111 | - | ||
| 112 | - | ||
| 113 | - | ||
| 114 | - | ||
| 115 | - | ||
| 116 | - | ||
| 117 | - | ||
| 118 | - | ||
| 119 | - | ||
| 120 | - | ||
| 121 | - | ||
| 122 | - | ||
| 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. | |
| 123 | 126 | ``` | |
| 124 | 127 | ||
| 125 | 128 | --- | |
| 126 | - | ||
| 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).* |
