Delphi结构体的方法


本文整理自网络,侵删。

 

Delphi结构体的方法与类的方法几乎是一致的,主要区别是内存的管理方式和可见性不同。

//定义
type
  TMyStruct = record
    No: Integer;
    Name: string;
    function ToString: string;
  end;
//实现
function TMyStruct.ToString: string;
begin
  Result := Format('No:%d, Name:%s', [Self.No, Self.Name]);
end;
//调用
var ms: TMyStruct; s: string;
begin
  s := ms.ToString;
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类型的方法是在结构体方法的基础上实现的。

来源:https://my.oschina.net/afrusrsc/blog/3136646

相关阅读 >>

Delphi 优盘背景生成器源码

Delphi 提高unigui开发效率的两个方法

Delphi winapi: messagebeep - 播放一个系统声音

Delphi firedac获取自增长字段值

Delphi 简单的软件注册demo

Delphi inifile to xml

Delphi tcombobox 设置默认值

Delphi 读取流 image1 stream 加载到image2 timage 对象

Delphi webbroker isapi 示例说明

Delphi 熊猫烧香核心源码

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...