delphi 使用泛型

type
  TForm8 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

type
  TmyKeyValue<T> = class
  private
    Fkey: string;
    Fvalue: T;
    procedure setKey(const Value: string);
    procedure setValue(const Value: T);
  public
    property Key: string read Fkey write setKey;
    property Value: T read Fvalue write setValue;
  end;

var
  Form8: TForm8;

implementation

{$R *.dfm}

{ TmyKeyValue<T> }

procedure TmyKeyValue<T>.setKey(const Value: string);
begin
  Fkey := Value;
end;

procedure TmyKeyValue<T>.setValue(const Value: T);
begin
  Fvalue := Value;
end;

procedure TForm8.Button1Click(Sender: TObject);
var
  kv: TmyKeyValue<TButton>;
begin
  kv := TmyKeyValue<TButton>.Create;
  kv.Key := 'myKey';
  kv.Value := Sender as TButton;

  ShowMessage(kv.Key + '||||||||' + kv.Value.Name);

  kv.Free;
end;

 

posted @ 2019-02-27 16:03  夏天的西瓜君  阅读(644)  评论(0编辑  收藏  举报