在第一篇的Delphi初浅入门笔记之一 :Object-Pascal基础中,我列出了一些高级数据类型。但是在实际的学习中,我只接触了枚举类型。还有字节类型,数组类型等等需要掌握,如果需要用到Delphi做开发的话。
还是用源代码来说事吧:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{
在这里定义了一个名为week的枚举类型,可以看到定义枚举使用type关键字来定义的
}
type
week=(sun,mon,tue,wed,thu,fri,sat);
{$R *.dfm}
{
这个函数返回string类型,它的作用是将枚举类型的值转换为友好的字符串形式。
}
function mday(day:week):string;
begin
case day of
sun:mday:='星期天';
mon:mday:='星期一';
tue:mday:='星期二';
wed:mday:='星期三';
thu:mday:='星期四';
fri:mday:='星期五';
sat:mday:='星期六';
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
year,month,day:word;
begin
decodedate(date,year,month,day);
edit1.Text:=format('%d年%d月%d日',[year,month,day]);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
today,yesterday,tomorrow:week;
n:Integer;
begin
n:=dayofweek(now);
case n of
1:today:=sun;
2:today:=mon;
3:today:=tue;
4:today:=wed;
5:today:=thu;
6:today:=fri;
7:today:=sat;
end;
if today=low(week) then
yesterday:=high(week)
else
yesterday:=pred(today);
if today=high(week) then
tomorrow:=low(week)
else
tomorrow:=succ(today);
button1.Caption:='昨天';
button2.Caption:='今天';
button3.Caption:='明天';
case (sender as tButton).Tag of
0:button1.Caption:='昨天是'+ mday(yesterday);
1:button2.Caption:='今天是'+ mday(today);
2:button3.Caption:='明天是'+ mday(tomorrow);
end;
end;
end.