Delphi 助手Helpers的用法

界面:

代码

unit Unit6;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

{ type
   identifierName = class helper [(ancestor list)] for classTypeIdentifierName
    ..........
   end;}

 type
 TListboxHelper = class helper for TListBox
 function ItemIndexValue: string;
 end;
 {类别助手最明显的限制,就是我们每次对一个类别只能使用一个助手(保证其唯一性)。如
果编译程序发现了有两个助手类别,第二个助手类别就会把第一个给取代
掉。所以也没有任何方法可以对类别助手进行连锁使用,也就是说我们不
能对类别助手再制作另一个类别助手。}
type
  TForm6 = class(TForm)
    ListBox1: TListBox;
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form6: TForm6;

implementation

{$R *.dfm}
 function TListboxHelper.ItemIndexValue: string;
begin
 Result := '';
 if ItemIndex >= 0 then
 Result := Items [ItemIndex];
end;
procedure TForm6.Button1Click(Sender: TObject);  //不用助手
begin
SHOWMESSAGE(ListBox1.Items [ListBox1.ItemIndex]);
end;

procedure TForm6.Button2Click(Sender: TObject);
begin
    SHOWMESSAGE (ListBox1.ItemIndexValue);  //使用助手  ------>扩展了方法
end;

end.
{包含在 SysUtils 单元文件里面的 助手 :
TStringHelper = record helper for string
TSingleHelper = record helper for Single
TDoubleHelper = record helper for Double
TExtendedHelper = record helper for Extended
TByteHelper = record helper for Byte
TShortIntHelper = record helper for ShortInt
TSmallIntHelper = record helper for SmallInt
TWordHelper = record helper for Word
TCardinalHelper = record helper for Cardinal
TIntegerHelper = record helper for Integer
TInt64Helper = record helper for Int64
TUInt64Helper = record helper for UInt64
TNativeIntHelper = record helper for NativeInt
TNativeUIntHelper = record helper for NativeUInt
TBooleanHelper = record helper for Boolean
TByteBoolHelper = record helper for ByteBool
TWordBoolHelper = record helper for WordBool
TLongBoolHelper = record helper for LongBool
TWordBoolHelper = record helper for WordBool}

猜你喜欢

转载自blog.csdn.net/qq_25439957/article/details/89468659