了解Delphi中的泛型类型

Generics, a powerful addition to Delphi, were introduced in Delphi 2009 as a new language feature. Generics or generic types (also know as parametrized types), allow you to define classes that don't specifically define the type of certain data members.

泛型是Delphi的强大补充,在Delphi 2009中作为一种新的语言功能被引入。 泛型或泛型类型(也称为参数化类型 )允许您定义未专门定义某些数据成员类型的类。

As an example, instead of using the TObjectList type to have a list of any object types, from Delphi 2009, the Generics. Collections unit defines a more strongly typed TObjectList.

例如,从Delphi 2009的泛型中 ,不使用TObjectList类型获取任何对象类型的列表 集合单元定义了一个更强类型的TObjectList。

Here's a list of articles explaining generic types in Delphi with usage examples:

这是解释使用Delphi中的泛型类型和用法示例的文章列表:

什么以及为什么以及如何在Delphi中泛型 ( What and Why and How on Generics in Delphi )

Delphi 2009 Win32的泛型 ( Generics with Delphi 2009 Win32 )

Generics are sometimes called generic parameters, a name which allows to introduce them somewhat better. Unlike a function parameter (argument), which has a value, a generic parameter is a type. And it parameterizes a class, an interface, a record, or, less frequently, a method ... With, as a bonus, anonymous routines and routine references

泛型有时称为泛型参数,该名称允许对其进行更好的介绍。 与具有值的函数参数(参数)不同,泛型参数是类型。 并且它参数化一个类,一个接口,一条记录或一个(较少见的)一个方法...作为奖励,匿名例程和例程引用

Delphi泛型教程 ( Delphi Generics Tutorial )

Delphi tList, tStringList, tObjectlist or tCollection can be used to build specialized containers, but require typecasting. With Generics, casting is avoided and the compiler can spot type errors sooner.

Delphi tList,tStringList,tObjectlist或tCollection可用于构建专用容器,但需要进行类型转换。 使用泛型,可以避免强制转换,并且编译器可以更快地发现类型错误。

在Delphi中使用泛型 ( Using Generics in Delphi )

Once you’ve written a class using generic type parameters (generics), you can use that class with any type and the type you choose to use with any given use of that class replaces the generic types you used when you created the class.

使用泛型类型参数(泛型)编写类后,便可以将该类与任何类型一起使用,并且选择与该类的任何给定用途一起使用的类型将替换您在创建该类时使用的泛型类型。

Delphi中的通用接口 ( Generic Interfaces in Delphi )

Most of the examples I’ve seen of Generics in Delphi use classes containing a generic type. However, while working on a personal project, I decided I wanted an Interface containing a generic type.

我在Delphi中看到的有关泛型的大多数示例都使用包含泛型的类。 但是,在进行个人项目时,我决定要一个包含泛型的接口。

简单泛型类型示例 ( Simple Generics Type Example )

Here's how to define a simple generic class:

以下是定义简单泛型类的方法:

type 类型
TGenericContainer<T> =
TGenericContainer <T> = class
Value : T;
值:T; end; 结束 ;

With the following definition, here's how to use an integer and string generic container:

通过以下定义,以下是使用整数和字符串通用容器的方法:

var 变种
genericInt : TGenericContainer<integer>;
genericInt:TGenericContainer <整数>;
genericStr : TGenericContainer<string>;
genericStr:TGenericContainer <字符串>; begin 开始
genericInt := TGenericContainer<integer>.Create;
genericInt:= TGenericContainer <integer> .Create;
genericInt.Value := 2009;
genericInt.Value:= 2009; //only integers //仅整数
genericInt.Free;
genericInt.Free;
genericStr := TGenericContainer<string>.Create;
genericStr:= TGenericContainer <string> .Create;
genericStr.Value := 'Delphi Generics';
genericStr.Value:='Delphi泛型'; //only strings //只有字符串
genericStr.Free;
genericStr.Free; end; 结束 ;

The above example only scratches the surface of using Generics in Delphi (does not explain anything though - but above articles have it all you want to know!).

上面的示例仅涉及在Delphi中使用泛型的表面(尽管并没有解释任何内容-但以上文章具有您想知道的全部信息!)。

For me, generics were the reason to move from Delphi 7 / 2007 to Delphi 2009 (and newer).

对我而言,泛型是从Delphi 7/2007迁移到Delphi 2009(及更高版本)的原因。

翻译自: https://www.thoughtco.com/understanding-generic-types-in-delphi-1058229

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值