TCollection/de: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: TCollection/de
| Old | New | Line | |
|---|---|---|---|
| 1 | + | Zurück zur Seite [[code examples/de|Code Beispiele]]. | |
| 2 | + | ||
| 3 | + | Collections gehören zur objektorientierten Programmierung. | |
| 4 | + | ||
| 5 | + | Collections sind Klassen und den Arrays ähnlich. | |
| 6 | + | ||
| 7 | + | Erstellen der benötigten Klassen: | |
| 8 | + | ||
| 9 | + | ``` pascal | |
| 10 | + | unit uCollections; | |
| 11 | + | ||
| 12 | + | {$mode objfpc}{$H+} | |
| 13 | + | ||
| 14 | + | interface | |
| 15 | + | ||
| 16 | + | uses | |
| 17 | + | Classes; | |
| 18 | + | ||
| 19 | + | type | |
| 20 | + | ||
| 21 | + | // TListItem | |
| 22 | + | // TListitem ist eine Unterelement von TListe | |
| 23 | + | TListItem = class(TCollectionItem) | |
| 24 | + | private | |
| 25 | + | strVorname: string; | |
| 26 | + | strNachname: string; | |
| 27 | + | bytAlter: byte; | |
| 28 | + | public | |
| 29 | + | // überschreibt den von TCollection geerbten Construktor | |
| 30 | + | constructor Create(colCollection: TCollection); override; | |
| 31 | + | property Vorname: string read strVorname write strVorname; | |
| 32 | + | property Nachname: string read strNachname write strNachname; | |
| 33 | + | property Alter: byte read bytAlter write bytAlter; | |
| 34 | + | //.. beliebige viele Properties können folgen | |
| 35 | + | end; | |
| 36 | + | ||
| 37 | + | // TListe | |
| 38 | + | // An TListe werden die Elemente von TListItem angehängt | |
| 39 | + | TListe = class(TCollection) | |
| 40 | + | private | |
| 41 | + | function GetItems(index: integer): TListItem; | |
| 42 | + | procedure SetItems(index: integer; Value: TListItem); | |
| 43 | + | public | |
| 44 | + | constructor Create; | |
| 45 | + | // Die Funktion hängt an das Objekt vom Typ TListe | |
| 46 | + | // ein Objekt vom Typ TCollectionItem hinzu. | |
| 47 | + | function Add: TCollectionItem; | |
| 48 | + | property Items[index: integer]: TListItem read GetItems write SetItems; | |
| 49 | + | end; | |
| 50 | + | ||
| 51 | + | implementation | |
| 52 | + | ||
| 53 | + | // TListe | |
| 54 | + | ||
| 55 | + | function TListe.Add: TCollectionItem; | |
| 56 | + | begin | |
| 57 | + | // Es können beliebig viele Objekte vom Typ TCollectionItem, | |
| 58 | + | // als Elemente angefügt werden. | |
| 59 | + | Result := TCollectionItem(inherited Add); | |
| 60 | + | end; | |
| 61 | + | ||
| 62 | + | constructor TListe.Create; | |
| 63 | + | begin | |
| 64 | + | inherited Create(TListItem); | |
| 65 | + | end; | |
| 66 | + | ||
| 67 | + | function TListe.GetItems(index: integer): TListItem; | |
| 68 | + | begin | |
| 69 | + | Result := TListItem(inherited Items[Index]); | |
| 70 | + | end; | |
| 71 | + | ||
| 72 | + | procedure TListe.SetItems(index: integer; Value: TListItem); | |
| 73 | + | begin | |
| 74 | + | Items[Index].Assign(Value); | |
| 75 | + | end; | |
| 76 | + | ||
| 77 | + | // TListItem | |
| 78 | + | ||
| 79 | + | constructor TListItem.Create(colCollection: TCollection); | |
| 80 | + | begin | |
| 81 | + | if assigned(colCollection) and (colCollection is TListe) then | |
| 82 | + | inherited Create(colCollection); | |
| 83 | + | end; | |
| 84 | + | ||
| 85 | + | end. | |
| 86 | + | ``` | |
| 87 | + | ||
| 88 | + | Das Objekt erstellen, füllen und auslesen: | |
| 89 | + | ||
| 90 | + | ``` pascal | |
| 91 | + | procedure subVerarbeitung; | |
| 92 | + | var | |
| 93 | + | Liste: TListe; | |
| 94 | + | strVorname: string; | |
| 95 | + | ||
| 96 | + | begin | |
| 97 | + | ||
| 98 | + | .... | |
| 99 | + | ||
| 100 | + | // erstellt die Collection | |
| 101 | + | Liste := TListe.Create; | |
| 102 | + | ||
| 103 | + | ... | |
| 104 | + | ||
| 105 | + | // füllt die die Collection mit Daten | |
| 106 | + | Liste.Add; | |
| 107 | + | Liste.Items[Liste.Count-1].Vorname := 'Frida'; // ein String | |
| 108 | + | Liste.Items[Liste.Count-1].Nachname := 'Fritz'; // ein String | |
| 109 | + | Liste.Items[Liste.Count-1].Alter := 22; // ein Byte | |
| 110 | + | ||
| 111 | + | Liste.Add; | |
| 112 | + | Liste.Items[Liste.Count-1].Vorname := 'Karl'; // ein String | |
| 113 | + | Liste.Items[Liste.Count-1].Nachname := 'Hamster'; // ein String | |
| 114 | + | Liste.Items[Liste.Count-1].Alter := 43; // ein Byte | |
| 115 | + | ||
| 116 | + | ... | |
| 117 | + | ||
| 118 | + | // liest ein Element der Collection aus | |
| 119 | + | strVorname:= Liste.Items[0].Vorname; | |
| 120 | + | ||
| 121 | + | ... | |
| 122 | + | ||
| 123 | + | // gibt den von der Collection belegten Speicher wieder frei | |
| 124 | + | Liste.Free; | |
| 125 | + | ||
| 126 | + | end; | |
| 127 | + | ``` | |
| 128 | + | ||
| 129 | + | --- | |
| 130 | + | *From the Free Pascal wiki, page "TCollection/de", as it stood on 2020-02-29 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).* |
