Java和C#里面的For In用起来真爽,我们Delphin也不用眼红了,从D2005起,Delphi里面也有这个功能了.
首先我们要知道哪些类型可以用For In吧,下面就是:
- for Element in ArrayExpr do Stmt; 数组
- for Element in StringExpr do Stmt; 字符串
- for Element in SetExpr do Stmt; 集合
- for Element in CollectionExpr do Stmt; 集合
- for Element in Record do Stmt; 结构体
我们来看例子:
view source
?02 | THuangJacky = (hjA,hjB,hjC,hjD); |
07 | stringExpr= 'HuangJacky' ; |
08 | arrayExpr: array [ 0..5 ] of Integer = ( 1 , 2 , 3 , 4 , 5 , 6 ); |
09 | setExpr: set of THuangJacky = [hjA,hjB,hjD]; |
10 | procedure TForm1 . FormCreate(Sender: TObject); |
20 | ShowMessage(IntToStr(i)); |
22 | ShowMessage(IntToStr(Ord(d))); |
是不是很爽呀?哈哈,Delphi也与时俱进呀.
之前写了类助手文章中,老赵问是不是扩展方法,因为对C#没有了解到这么多,所以不知道.
那么我们在Java中要For In必须实现Iterator吧.
那么Delphi的会不会也要呢?
是的,如果我们要自己的类支持For In的话,就必须满足下面的条件:
1 必须有个公共方法GetEnumerator(),这个方法返回值是一个类,接口或者记录体.
2 上面返回的类,接口或者记录体中又必须有公共方法MoveNext(),这个方法的返回值是Boolean.
3 1中返回的类,接口或者记录体中必须有一个只读的属性Current,类型要和集合中的元素一样.
说了这么多,看个例子:
view source
?02 | TMyIntArray = array of Integer ; |
09 | function GetCurrent: Integer ; |
10 | function MoveNext: Boolean ; |
11 | property Current: Integer read GetCurrent; |
16 | function GetEnumerator: TMyEnumerator; |
19 | constructor TMyEnumerator . Create; |
22 | Values := TMyIntArray . Create( 100 , 200 , 300 ); |
26 | function TMyEnumerator . MoveNext: Boolean ; |
28 | if Index < High(Values) then |
37 | function TMyEnumerator . GetCurrent: Integer ; |
39 | Result := Values[Index]; |
42 | function TMyContainer . GetEnumerator: TMyEnumerator; |
44 | Result := TMyEnumerator . Create; |
48 | MyContainer: TMyContainer; |
54 | MyContainer := TMyContainer . Create; |
60 | WriteLn ( 'Counter = ' , Counter); |