Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Delphi:通用类定义,异步新手_Delphi_Asynchronous_Sample_Generic Collections - Fatal编程技术网

Delphi:通用类定义,异步新手

Delphi:通用类定义,异步新手,delphi,asynchronous,sample,generic-collections,Delphi,Asynchronous,Sample,Generic Collections,我来自Delphi2007,已经使用它7年了,并开始在我的新工作场所与RAD Studio Berlin合作。我正在从事的项目使用了大量的字典、泛型集合、异步等等。 例如,有一个类: TMyBaseClass = class abstract(TObject) private ... end; TNewClassGeneric<T: TMyBaseClass> = class(TMyAbstractSuperClass) .... TMyBaseClass=类抽象(TObjec

我来自Delphi2007,已经使用它7年了,并开始在我的新工作场所与RAD Studio Berlin合作。我正在从事的项目使用了大量的字典、泛型集合、异步等等。 例如,有一个类:

TMyBaseClass = class abstract(TObject)
private
...

end;

TNewClassGeneric<T: TMyBaseClass> = class(TMyAbstractSuperClass)
....
TMyBaseClass=类抽象(TObject)
私有的
...
结束;
TNewClassGeneric=class(TMyAbstractSuperClass)
....
那么这是什么意思呢

我仍然在为这些新事物而挣扎。 我能读到更多关于泛型、字典和异步程序代码示例的信息吗?
谢谢

看看下面的类(它应该可以编译)。请注意,它只使用了一个genrict,没有在MyValue的属性定义中指定类型。这允许它在两个过程(UseGenreicInt和UseGnericString)中用作整数或字符串,以演示它的功能。 这就是泛型的力量。您可以定义一个类来处理数据,而无需事先知道您将处理什么类型的数据(int、string等)

unit Sample.Generic;

interface

uses
  System.SysUtils, System.Variants, System.Classes, System.Generics.Collections, VCL.Dialogs;

type
  TSampleGeneric<T> = class
  protected
    FValue: T;
  public
    constructor Create(AValue: T);
    property MyValue: T read FValue write FValue;
  end;

procedure UseGenricString;
procedure UseGenricInt;

implementation

constructor TSampleGeneric<T>.Create(AValue: T);
begin
  FValue := AValue;
end;

procedure UseGenricInt;
var
  LSample: TSampleGeneric<Integer>;
begin
  LSample := TSampleGeneric<Integer>.Create(100);
  try
    ShowMessage(IntToStr(LSample.MyValue));
  finally
    LSample.Free;
  end;
end;

procedure UseGenricString;
var
  LSample: TSampleGeneric<String>;
begin
  LSample := TSampleGeneric<String>.Create('ATestString');
  try
    ShowMessage(LSample.MyValue);
  finally
    LSample.Free;
  end;
end;

end.
unitsample.Generic;
接口
使用
System.SysUtils、System.Variant、System.Class、System.Generics.Collections、VCL.Dialogs;
类型
TSampleGeneric=class
受保护的
f值:T;
公众的
构造函数创建(AValue:T);
属性MyValue:T读取FValue写入FValue;
结束;
程序UseGenricString;
程序使用推理;
实施
构造函数TSampleGeneric.Create(AValue:T);
开始
f值:=有效值;
结束;
程序使用推理;
变量
LSample:tsample-generic;
开始
LSample:=TSampleGeneric.Create(100);
尝试
ShowMessage(IntToStr(LSample.MyValue));
最后
LSample.Free;
结束;
结束;
程序UseGenricString;
变量
LSample:tsample-generic;
开始
LSample:=TSampleGeneric.Create('ATestString');
尝试
ShowMessage(LSample.MyValue);
最后
LSample.Free;
结束;
结束;
结束。

啊……谢谢。你能告诉我在哪里可以读到更多关于Delphi中的泛型编程的信息吗?@Ricky你在用什么搜索引擎?您可能需要切换搜索引擎。对不起,您的意思是什么?这是一个推荐问题,因此无法访问帮助文件或其web版本:。