Discuz! BBS

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 540|回复: 0

初始化自定义类

[复制链接]

254

主题

363

帖子

2431

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
2431
发表于 2024-12-25 09:45:03 | 显示全部楼层 |阅读模式
  1. type
  2.   TMyClass = class
  3.   private
  4.     FValue: Integer;
  5.   public
  6.     constructor Create(InitialValue: Integer);
  7.     procedure DoSomething;
  8.   end;

  9. constructor TMyClass.Create(InitialValue: Integer);
  10. begin
  11.   inherited Create; // 如果TMyClass没有继承自其他类,这行可以省略
  12.   FValue := InitialValue;
  13. end;

  14. procedure TMyClass.DoSomething;
  15. begin
  16.   // 实现一些功能
  17.   ShowMessage('The value is ' + IntToStr(FValue));
  18. end;

  19. // 使用自定义类
  20. procedure TForm1.Button1Click(Sender: TObject);
  21. var
  22.   MyObj: TMyClass;
  23. begin
  24.   MyObj := TMyClass.Create(10); // 初始化类的实例
  25.   try
  26.     MyObj.DoSomething;
  27.   finally
  28.     MyObj.Free; // 释放资源
  29.   end;
  30. end;
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|DiscuzX

GMT+8, 2025-4-16 04:50 , Processed in 0.014262 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表