在Delphi中简单的调用单元unit实例


本文整理自网络,侵删。

 在Delphi中简单的调用单元unit实例 
一,新建一个工程文件,默认的文件是unit1,代码如下:unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

uses Unit2;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
nTemp:integer;
begin
nTemp:=add(3,4);
//也可以这样nTemp:=unit2.add(3,4);

edit1.Text:=inttostr(nTemp);
end;

end.



2,新建一个unit,默认名称是unit2,代码如下:

unit Unit2;

interface
uses windows,messages, SysUtils, Variants, Classes;
function add(a,b:integer):integer;

implementation

function add(a,b:integer):integer;
begin
result:=a+b;
end;

end.



从这里我们可以看到,单元的引用是非常简单的.我们的unit2只是一个代码单元,没有窗体.在interface节中我们相当于声明了单元的对外可见部分,在implementation中,定义了实现部分.在unit1当中,我们引用unit2后,直接可以调用add函数了,当然,也可以在前面加上unit2.add()这样的形式.在这个单元中,我们可以把一些公用函数,类等东西放进去,实现程序的模块化.便于程序结构明晰.也便于程序维护.

相关阅读 >>

Delphi case 语句中使用字符串

Delphi idpop3收邮件

Delphixe4 版本中,已针对移动平台 引入了 arc 模型

Delphi程序中使用自定义的鼠标

Delphi 自动关闭弹出的窗口

Delphi idhttpserver的使用方法

Delphi 之 trichedit组件

Delphi 批量日期格式化

Delphi spcomm控件的安装

Delphi 注入win7的单元

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



打赏

取消

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

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

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

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

评论

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