end;
类型的方法
Delphi
Delphi 2009以后,类型也可以有方法,最常用的是各基本类型的ToString方法。Integer类型的方法源码如下:
//Integer 类型的方法声明
TIntegerHelper = record helper for Integer { for LongInt type too }
public
const
MaxValue = 2147483647;
MinValue = -2147483648;
function ToString: string; overload; inline;
function ToBoolean: Boolean; inline;
function ToHexString: string; overload; inline;
function ToHexString(const MinDigits: Word): string; overload; inline;
function ToSingle: Single; inline;
function ToDouble: Double; inline;
function ToExtended: Extended; inline;
class function Size: Integer; inline; static;
class function ToString(const Value: Integer): string; overload; inline; static;
class function Parse(const S: string): Integer; inline; static;
class function TryParse(const S: string; out Value: Integer): Boolean; inline; static;
end;
//Integer 类型的 ToString 方法实现
function TIntegerHelper.ToString: string;
begin
Result := IntToStr(Self);
end;
可以看出,Delphi类型的方法是在结构体方法的基础上实现的。