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; 结构体
我们来看例子:
|
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
|
typeTHuangJackyTJackyHuangrecord a,b,c:Integer;end;const stringExpr='HuangJacky'; arrayExpr:array[0..5]of Integer=1,2,3,4,5,6); setExpr:set of THuangJackyprocedure TForm1.FormCreate(Sender:var I:Integer; C:Char; D:THuangJacky; F:TComponent;beginfor cin stringExprdo ShowMessage(C);for iin arrayExprdo ShowMessage(IntToStr(i));for din setExprdo ShowMessage(IntToStr(Ord(d)));for Fin Selfdo 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 TMyIntArrayarray of Integer; TMyEnumeratorclass Values: Index: Integer; public constructor Create; function GetCurrent:Integer; function MoveNext: Boolean; property Current: Integer read end; TMyContainer class public function GetEnumerator: end;constructor TMyEnumerator.Create;begin inherited Create; Values.Create(100,200,300); Index1;end;function TMyEnumerator.MoveNext:Boolean;begin if Indexthen begin Inc(Index); ResultTrue; end else ResultFalse;end;function TMyEnumerator.GetCurrent:Integer;begin Resultend;function TMyContainer.GetEnumerator:begin Result.Create;end;var MyContainer: I:Integer; Counter:Integer;begin MyContainer.Create; Counter0; for Iin MyContainerdo Inc(Counter, WriteLn('Counter,end. |
Delphi新语法 For ..In的更多相关文章
- delphi新语法之泛型实现的对象池模板
现在的DELPHI因为支持泛型的语法,所以也能支持模板编程了. // 标准模板 unit UntPools; interface uses Classes, SysUtils, Unt ...
- Delphi新语法
http://www.cnblogs.com/hnxxcxg/category/456344.html
- Delphi 7以来的Delphi 2009测试版新语法特性
我晕,Delphi 7 以后增加了这么多有用的语法,我都不知道.真是越学越觉得自己浅薄,自己所作的Delphi项目所用的知识还不够Delphi知识储备体系的十分之一,更别说Delphi还在继续发展. ...
- class helper 可能是从 Delphi 2007 增加的新语法
class helper 可能是从 Delphi 2007 增加的新语法, 因为感觉不太实用, 直到今天才测试了一下. 试过之后才知道: 挺有意思的! 基本功能就是修改已存在的类. Txxx = cl ...
- delphi c#语法转换
delphi c#语法转换 delphi c# s:array[1..5] of integer TIArr = array of integer; Berlin有这个新功能 TArra ...
- [C#] 回眸 C# 的前世今生 - 见证 C# 6.0 的新语法特性
回眸 C# 的前世今生 - 见证 C# 6.0 的新语法特性 序 目前最新的版本是 C# 7.0,VS 的最新版本为 Visual Studio 2017 RC,两者都尚未进入正式阶段.C# 6.0 ...
- qt5中信号和槽的新语法
qt5中的连接 有下列几种方式可以连接到信号上 旧语法 qt5将继续支持旧的语法去连接,在QObject对象上定义信号和槽函数,及任何继承QObjec的对象(包含QWidget). connect(s ...
- Qt 5.0+ 中 connect 新语法与重载函数不兼容问题的解决方法,以及个人看法
Qt 5.0+ 版本提供了 connect 的新语法,相比之前的语法新语法可以提供编译期检查,使用也更方便.可是使用过程中发现一个小问题——当某个 signal 和成员函数是重载关系的时候,qmake ...
- .NET中那些所谓的新语法之一:自动属性、隐式类型、命名参数与自动初始化器
开篇:在日常的.NET开发学习中,我们往往会接触到一些较新的语法,它们相对以前的老语法相比,做了很多的改进,简化了很多繁杂的代码格式,也大大减少了我们这些菜鸟码农的代码量.但是,在开心欢乐之余,我们也 ...
随机推荐
- luogu P1495 曹冲养猪(中国剩余定理)
题意 题解 翻到了一个金句 就跟这句话说得一样,就是个裸题. 所以看模板呗. #include<iostream> #include<cstring> #include< ...
- 【BZOJ 1588】 [HNOI2002]营业额统计
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 每天的最小波动值指的是和之前所有天的差值的绝对值中的最小值. 用set.的lower_bound函数. 每次找和他差值最小的数字就好 ...
- 传纸条 NOIP2008 洛谷1006 二维dp
二维dp 扯淡 一道比较基本的入门难度的二维dp,类似于那道方格取数,不过走过一次的点下次不能再走(看提交记录里面好像走过一次的加一次a[i][j]的也AC了,,),我记得当年那道方格取数死活听不懂, ...
- COGS——T 803. [USACO Hol10] 政党 || 1776: [Usaco2010 Hol]cowpol 奶牛政坛
http://www.lydsy.com/JudgeOnline/problem.php?id=1776||http://cogs.pro/cogs/problem/problem.php?pid=8 ...
- 【LeetCode】Longest Palindromic Substring 解题报告
DP.KMP什么的都太高大上了.自己想了个朴素的遍历方法. [题目] Given a string S, find the longest palindromic substring in S. Yo ...
- 杭电OJ(HDU)-ACMSteps-Chapter Two-《An Easy Task》《Buildings》《decimal system》《Vowel Counting》
http://acm.hdu.edu.cn/game/entry/problem/list.php?chapterid=1§ionid=2 1.2.5 #include<stdio.h> ...
- opecv2 MeanShift 使用均值漂移算法查找物体
#if !defined OFINDER #define OFINDER #include <opencv2\core\core.hpp> #include <opencv2\img ...
- 将一个文件夹纳入library或者移除remove
https://support.microsoft.com/en-us/help/4026298/windows-show-libraries-in-file-explorer To show lib ...
- spring boot 的常用注解使用 总结
附:Spring Boot 官方文档学习(一)入门及使用见https://www.cnblogs.com/larryzeal/p/5799195.html @RestController和@Reque ...
- 滑动切换Activity代码
最近需要对练习项目中的代码进行优化,发现很多代码写起来远比想象的困难很多.刚接触Android时间不长,很多东西都不能融会贯通,所以才会有这样的问题存在,当然学习中遇到的问题很有必要做个总结.想想这个 ...