Now you are going to create the items array attribute. Enter the following in the public area: Code :
Property items [index: integer]: pointer;
The code after executing SHIFT + Ctrl + C is:
... Tmylist = Class (tobject) Private... function getitems (Index: integer): pointer; Procedure setitems (Index: integer; const value: pointer); public... property items [index: integer]: pointer read getitems write setitems; end; implementation {tmylist }... function tmylist. getitems (Index: integer): pointer; beginend; Procedure tmylist. setitems (Index: integer; const value: pointer); beginend; end.
In the tlist class, the getitems method is named get; The setitems method is named put. We will not change the name here.
The implementation is as follows:
Function tmylist. getitems (Index: integer): pointer; begin if (Index = fcount) then raise exception. createfmt ('exception: % d', [Index]); Result: = flist ^ [Index]; end; {we didn't touch the notify method here, the current tmylist does not have this method} procedure tmylist. setitems (Index: integer; const value: pointer); begin if (Index = fcount) then raise exception. createfmt ('exception: % d', [Index]); If value flist ^ [Index] Then flist ^ [Index]: = value; end;
Now, we can access the elements in the list using the list. itmes [I] method;
Further, let it be the default attribute. Although only one attribute can be selected as the default attribute, which attribute is more important than the default attribute?
// You only need to change the value declared in the public area: property items [index: integer]: pointer read getitems write setitems; // to property items [index: integer]: pointer read getitems write setitems; default;
Items is the default attribute. when accessing an element, you can use: List. itmes [I] Or: list [I]. The default attribute is really convenient.
Let's take a look at all the current code of the tmylist class:
Unit mylist; interfaceuses sysutils; const maxlistsize = maxint Div 16; Type ppointerlist = ^ tpointerlist; tpointerlist = array [0 .. maxlistsize-1] of pointer; tmylist = Class (tobject) Private flist: ppointerlist; fcount: integer; fcapacity: integer; Procedure setcapacity (const value: integer ); procedure setcount (const value: integer); function getitems (Index: integer): pointer; Procedure setitems (Index: integer; const value: pointer); Public destructor destroy; override; function add (item: pointer): integer; Procedure clear; Procedure Delete (Index: integer); property capacity: integer read fcapacity write setcapacity; property count: integer read fcount write setcount; property list: ppointerlist read flist; property items [index: integer]: pointer read getitems write setitems; default; end; implementation {tmylist} function tmylist. add (item: pointer): integer; begin if fcount = fcapacity then setcapacity (fcapacity + 4); flist ^ [fcount]: = item; Result: = fcount; INC (fcount); end; Procedure tmylist. clear; begin setcount (0); setcapacity (0); end; Procedure tmylist. delete (Index: integer); begin if (Index = fcount) then raise exception. createfmt ('invalid index: % d', [Index]); If index maxlistsize) then raise exception. createfmt ('invalid data: % d', [value]); If fcapacity value then begin reallocmem (flist, value * sizeof (pointer); fcapacity: = value; end; end; Procedure tmylist. setcount (const value: integer); var I: integer; begin if (value maxlistsize) then raise exception. createfmt ('invalid data: % d', [value]); If value> fcapacity then setcapacity (value); If value> fcount then fillchar (flist ^ [fcount], (value-fcount) * sizeof (pointer), 0) else for I: = fcount-1 downto value do Delete (I); fcount: = value; end; function tmylist. getitems (Index: integer): pointer; begin if (Index = fcount) then raise exception. createfmt ('exception: % d', [Index]); Result: = flist ^ [Index]; end; Procedure tmylist. setitems (Index: integer; const value: pointer); begin if (Index = fcount) then raise exception. createfmt ('exception: % d', [Index]); If value flist ^ [Index] Then flist ^ [Index]: = value; end.
Now the access element is convenient. Redo the previous test:
Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls; Type tform1 = Class (tform) Procedure formcreate (Sender: tobject ); end; var form1: tform1; implementation {$ R *. DFM} uses mylist; Type tmyrec = Record Name: String [8]; age: word; end; Procedure tform1.formcreate (Sender: tobject); var lista: tmylist; R, R1, r2, R3, R4, R5: tmyrec; begin lista: = tmylist. create; r1.name: = 'zhang san'; r1.age: = 11; lista. add (@ R1); r2.name: = 'lily'; r2.age: = 22; lista. add (@ R2); r3.name: = 'wang 5'; r3.age: = 33; lista. add (@ R3); r4.name: = 'Sun 6'; r4.age: = 44; lista. add (@ R4); r5.name: = 'hou 7'; r5.age: = 55; lista. add (@ R5); {get the third element} // R: = tmyrec (lista. list ^ [2] ^); {This is the previous Code} r: = tmyrec (lista [2] ^); showmessagefmt ('% s: % d', [R. name, R. age]); {Wang Wu: 33} {Delete the third element and then access the third element} lista. delete (2); // R: = tmyrec (lista. list ^ [2] ^); {This is the previous Code} r: = tmyrec (lista [2] ^); showmessagefmt ('% s: % d', [R. name, R. age]); {sun 6: 44} {now using the items attribute, you can not only set values, but also assign values} lista [2] :=@ R1; R: = tmyrec (lista [2] ^); showmessagefmt ('% s: % d', [R. name, R. age]); {Zhang San: 11} lista. free; end.