Delphi集合的用法

unit Unit4;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TEnum = (One, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten);
  TSetEnum = set of TEnum;
  TForm4 = class(TForm)
    GroupBox1: TGroupBox;
    Button1: TButton;
    Button2: TButton;
    GroupBox2: TGroupBox;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    Button7: TButton;
    Button8: TButton;
    Button9: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure Button9Click(Sender: TObject);
  private
    SetEnum, SetEnum1, SetEnum2: TSetEnum ;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form4: TForm4;

implementation

{$R *.dfm}

procedure TForm4.Button1Click(Sender: TObject);
var
  s1, s2: string;
begin
  s1 := 'aaa';
  s2 := s1;
  s2 := 'bbb';
  ShowMessage('字符串S1的值:  ' + s1 + ' ; 字符串S2的值: ' + S2);

end;

procedure TForm4.Button2Click(Sender: TObject);
var
  A1, A2: array of string;
begin
  SetLength(A1, 1);
  A1[0] := 'aaa';
  A2 := A1;
  A2[0] := 'bbb';
  ShowMessage('数组A1的值:' + A1[0]+ ' ;数组A2的值:' + A2[0]);
end;

//初始化
procedure TForm4.Button3Click(Sender: TObject);
begin
  SetEnum  :=  [one, Two];
  SetEnum1 := [Three, Nine, Ten]
end;

//集合减少
procedure TForm4.Button5Click(Sender: TObject);
begin
  SetEnum := SetEnum - [Two];
end;

//集合增加
procedure TForm4.Button6Click(Sender: TObject);
begin
  SetEnum := SetEnum + [Three];
end;

//集合减少
procedure TForm4.Button7Click(Sender: TObject);
begin
  Exclude(SetEnum, One);
end;

//集合增加
procedure TForm4.Button4Click(Sender: TObject);
begin
  Include(SetEnum, Four);
  Include(SetEnum, Ten);
end;

//执行校验
procedure TForm4.Button8Click(Sender: TObject);
begin
  if One in SetEnum then
  begin
    ShowMessage('Exclude函数执行失败');
  end
  else if Two in SetEnum then
  begin
    ShowMessage('SetEnum - [Two]执行失败');
  end
  else if not (Three in SetEnum) then
  begin
    ShowMessage('SetEnum + [Three]执行失败');
  end
  else if not (Four in SetEnum) then
  begin
    ShowMessage('Include函数执行失败');
  end

  else if not (ten in SetEnum) then
  begin
    ShowMessage('Include函数执行失败');
  end
  else
  begin
    ShowMessage('执行成功!');
  end;
end;

//取得集合的交集
procedure TForm4.Button9Click(Sender: TObject);
begin
  SetEnum2 := SetEnum1 * SetEnum;
  if Three in SetEnum2 then
  begin
    ShowMessage('交集成功执行成功');
  end;
end;

end.

//http://www.cnblogs.com/zhangzhifeng/archive/2011/08/03/2126010.html

posted @ 2013-05-25 19:07  梁彦坤  阅读(368)  评论(0编辑  收藏  举报