首先我们要知道哪些类型可以用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. c++PrimerChap8IO库

    #include<iostream> #include<fstream> #include<string> using namespace std; int mai ...

  2. 题解 P3128 【[USACO15DEC]最大流Max Flow】

    此类型题目有两种比较常见的做法:树链剖分和树上差分. 本题有多组修改一组询问,因此树上差分会比树链剖分优秀很多. 这里两种方法都进行介绍. 树链剖分和树上差分的本质都是将一颗树转换为一个区间,然后进行 ...

  3. js 函数基础(方便复习使用)

    // 函数声明: function bbq(){ // ..... } // 函数表达式: // 1.命名函数表达式 var test = function abc(){ document.write ...

  4. SpringMVC+Jquery -页面异步载入数据

    背景: 做项目时涉及到页面.当我打算在controller中传一个list到页面,然后通过<c:foreach>循环遍历出来时,同事说:你这样每次都要刷新.这都是几百年前使用的技术了.你用 ...

  5. 一、奇妙插件Tampermonkey的简单安装教程

    奇妙插件Tampermonkey的简单安装教程 1.下载 对于浏览器而言,一般的功能并不能满足"特殊用户的需求".这时候就须要插件来帮忙了.那么讲到插件的支持多又常见的浏览器必定要 ...

  6. URL长链接转换为短链接

    URL长链接转换为段链接的工具非常多,可是.小编还是要给大家唠一种方法的: 操作过程例如以下,打开腾讯微博或者其它微,将自己的URL地址值按图片操作:

  7. invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause

    Column 'dbo.tbm_vie_View.ViewID' is invalid in the select list because it is not contained in either ...

  8. Storm Zookeeper

    本文记录了storm 1.1.0 在zookeeper中保存的信息. 下面的图是在[4]的基础上进行修改的. /-storm -- storm在zookeeper上的根目录 | |-/assignme ...

  9. 用fcntl锁一个文件来保护操作

    int testfd; /* fd for test*/ if((testfd = open("/usr/local/pgsql/bin/test_fd",O_RDWR|O_CRE ...

  10. 【C#Windows 服务】 《三》Timer设置

    一.工具: VS2015+NET Framework4.5. 二.操作: 1.计时器设置: 2.日志代码: 三.代码: 1.日志代码: 1 /// <summary> 2 /// Wind ...