首先我们要知道哪些类型可以用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;         结构体

我们来看例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
type
THuangJacky
= (hjA,hjB,hjC,hjD);
TJackyHuang
=
record
    a,b,c:Integer;
end;
const
    stringExpr='HuangJacky';
    arrayExpr:array[0..5]
of Integer=
(
1,2,3,4,5,6);
    setExpr:set of THuangJacky
= [hjA,hjB,hjD];
procedure TForm1.FormCreate(Sender:
TObject);
var
    I:Integer;
    C:Char;
    D:THuangJacky;
    F:TComponent;
begin
for c
in stringExpr
do
    ShowMessage(C);
for i
in arrayExpr
do
    ShowMessage(IntToStr(i));
for d
in setExpr
do
    ShowMessage(IntToStr(Ord(d)));
for F
in Self
do
    ShowMessage(f.Name);
end;

是不是很爽呀?哈哈,Delphi也与时俱进呀.

之前写了类助手文章中,老赵问是不是扩展方法,因为对C#没有了解到这么多,所以不知道.
那么我们在Java中要For In必须实现Iterator吧.
那么Delphi的会不会也要呢?
是的,如果我们要自己的类支持For In的话,就必须满足下面的条件:
1 必须有个公共方法GetEnumerator(),这个方法返回值是一个类,接口或者记录体.
2 上面返回的类,接口或者记录体中又必须有公共方法MoveNext(),这个方法的返回值是Boolean.
3 1中返回的类,接口或者记录体中必须有一个只读的属性Current,类型要和集合中的元素一样.

说了这么多,看个例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
61
type
  TMyIntArray
=
array of Integer;
 
  TMyEnumerator
=
class
    Values:
TMyIntArray;
    Index: 
Integer;
  public
    constructor Create;
    function GetCurrent:
Integer;
    function MoveNext:  
Boolean;
    property Current:   
Integer read
GetCurrent;
  end;
 
  TMyContainer 
=
class
  public
   function GetEnumerator:
TMyEnumerator;
  end;
 
constructor TMyEnumerator.Create;
begin
  inherited Create;
  Values
:= TMyIntArray
.Create(100,
200,
300);
  Index
:= -
1;
end;
 
function TMyEnumerator.MoveNext:
Boolean;
begin
  if Index
< High(Values)
then
    begin
      Inc(Index);
      Result
:=
True;
    end
  else
    Result
:=
False;
end;
 
function TMyEnumerator.GetCurrent:
Integer;
begin
  Result
:= Values[Index];
end;
 
function TMyContainer.GetEnumerator:
TMyEnumerator;
begin
  Result
:= TMyEnumerator
.Create;
end;
 
var
  MyContainer:
TMyContainer;
  I:
Integer;
 
  Counter:
Integer;
 
begin
  MyContainer
:= TMyContainer
.Create;
 
  Counter
:=
0;
  for I
in MyContainer
do
    Inc(Counter,
I);
 
  WriteLn('Counter
= '
,
Counter);
end.

 
https://blog.csdn.net/rocklee/article/details/48627165

Delphi新语法 For ..In的更多相关文章

  1. delphi新语法之泛型实现的对象池模板

    现在的DELPHI因为支持泛型的语法,所以也能支持模板编程了.   // 标准模板 unit UntPools;   interface   uses   Classes, SysUtils, Unt ...

  2. Delphi新语法

    http://www.cnblogs.com/hnxxcxg/category/456344.html

  3. Delphi 7以来的Delphi 2009测试版新语法特性

    我晕,Delphi 7 以后增加了这么多有用的语法,我都不知道.真是越学越觉得自己浅薄,自己所作的Delphi项目所用的知识还不够Delphi知识储备体系的十分之一,更别说Delphi还在继续发展. ...

  4. class helper 可能是从 Delphi 2007 增加的新语法

    class helper 可能是从 Delphi 2007 增加的新语法, 因为感觉不太实用, 直到今天才测试了一下. 试过之后才知道: 挺有意思的! 基本功能就是修改已存在的类. Txxx = cl ...

  5. delphi c#语法转换

    delphi c#语法转换 delphi c#       s:array[1..5] of integer TIArr  = array of integer; Berlin有这个新功能 TArra ...

  6. [C#] 回眸 C# 的前世今生 - 见证 C# 6.0 的新语法特性

    回眸 C# 的前世今生 - 见证 C# 6.0 的新语法特性 序 目前最新的版本是 C# 7.0,VS 的最新版本为 Visual Studio 2017 RC,两者都尚未进入正式阶段.C# 6.0 ...

  7. qt5中信号和槽的新语法

    qt5中的连接 有下列几种方式可以连接到信号上 旧语法 qt5将继续支持旧的语法去连接,在QObject对象上定义信号和槽函数,及任何继承QObjec的对象(包含QWidget). connect(s ...

  8. Qt 5.0+ 中 connect 新语法与重载函数不兼容问题的解决方法,以及个人看法

    Qt 5.0+ 版本提供了 connect 的新语法,相比之前的语法新语法可以提供编译期检查,使用也更方便.可是使用过程中发现一个小问题——当某个 signal 和成员函数是重载关系的时候,qmake ...

  9. .NET中那些所谓的新语法之一:自动属性、隐式类型、命名参数与自动初始化器

    开篇:在日常的.NET开发学习中,我们往往会接触到一些较新的语法,它们相对以前的老语法相比,做了很多的改进,简化了很多繁杂的代码格式,也大大减少了我们这些菜鸟码农的代码量.但是,在开心欢乐之余,我们也 ...

随机推荐

  1. wordpress 后台登录增加访问效验,优化退出效果

    之前记录了 wordpress 后台登录增加访问效验, 记录了增加后台访问地址被直接访问的困难性的修改步骤. 还有一个地方需要补充一下,就是退出. 退出的时候不做调整会直接跳到首页,这样体验很不好. ...

  2. 线性规划(LP)资料下载

    1.学习用PPT harvard gondzio IOE610 mit cxg286 含matlab程序 2.测试库 BPMPD netlib fsu 3.软件测试 BENCHMARKS FOR OP ...

  3. 2015 Multi-University Training Contest 2 hdu 5306 Gorgeous Sequence

    Gorgeous Sequence Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  4. HDOJ 2828 Lamp DLX反复覆盖

    DLX反复覆盖模版题: 每一个开关两个状态.但仅仅能选一个,建2m×n的矩阵跑DLX模版.. .. Lamp Time Limit: 2000/1000 MS (Java/Others)    Mem ...

  5. angularjs 指令2

    <!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="C ...

  6. html+css实现选项卡功能

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. Lists are mutable

    The syntax for accessing the elements of a list is the same as for accessing the characters of a str ...

  8. Conditionals

    1. Modulus operator (%) The modulus operator works on integers and yields the remainder when the fir ...

  9. POJ 3173 模拟

    按照题意模拟就好-- //By SiriusRen #include <cstdio> #include <algorithm> using namespace std; in ...

  10. 关于概率算法的问题,不知道逻辑错在哪里,求debug

    做个骰子成功几率的分析,投n颗骰子,第一次投成功的几率是a,然后投成功的骰子,需要再投1次,这次成功的几率是b.第二次成功的骰子才算最终成功. 要分析出n颗骰子,最终成功0到n颗的概率. 我写了个算法 ...