Delphi 函数的重载和作用域
2009-06-05 09:48
169 查看
1.在delphi 中,我们可以使用相同的函数名来调用不同的函数,我们称这个函数为重载,函数的参数类型和参数的个数可以不同,用到的关键字overload;格式如下:
function addInt(x,y:integer):string;overload;
begin
result := intToStr(x + y);
end;
function addInt(x,y,z:integer):string;overload;
begin
result := intToStr(x + y + z);
end;
function addInt(x,y:integer;z:string):string;overload;
begin
result := intToStr(x + y + strToInt(z));
end;
注意点:每个函数后面都需要加上:overload;
2.函数的作用域(局部和全局函数):
在单位文件下声明的函数只能应用于本单元,可以通过其他单元引用来使用函数;但是必须在被引用单位的接口部分声明函数,否则还是无法找到
如:
Unit1.pas文件
unit Unit1;
interface
uses
windows, Messages, SysUtils;
uses
unit2; //可以和上面的uses一起连写
implementation
procedure TForm1.Button1Click(Sender : TOBJect);
begin
showmessage(addint(1,2,3));
end;
end.
Unit2.pas文件:
unit Unit2;
interface
uses
windows, Messages, SysUtils;
function addint(x,y,z:Integer):string; //必须在这里声明函数,才能被其他单位去引用
implementation
function addInt(x,y,z : integer):string;
begin
Result := intToStr(x+y+z);
end;
end.
function addInt(x,y:integer):string;overload;
begin
result := intToStr(x + y);
end;
function addInt(x,y,z:integer):string;overload;
begin
result := intToStr(x + y + z);
end;
function addInt(x,y:integer;z:string):string;overload;
begin
result := intToStr(x + y + strToInt(z));
end;
注意点:每个函数后面都需要加上:overload;
2.函数的作用域(局部和全局函数):
在单位文件下声明的函数只能应用于本单元,可以通过其他单元引用来使用函数;但是必须在被引用单位的接口部分声明函数,否则还是无法找到
如:
Unit1.pas文件
unit Unit1;
interface
uses
windows, Messages, SysUtils;
uses
unit2; //可以和上面的uses一起连写
implementation
procedure TForm1.Button1Click(Sender : TOBJect);
begin
showmessage(addint(1,2,3));
end;
end.
Unit2.pas文件:
unit Unit2;
interface
uses
windows, Messages, SysUtils;
function addint(x,y,z:Integer):string; //必须在这里声明函数,才能被其他单位去引用
implementation
function addInt(x,y,z : integer):string;
begin
Result := intToStr(x+y+z);
end;
end.
相关文章推荐
- Delphi 函数的重载和作用域
- C++中函数占位符在函数重载中的作用
- C++ 和 Delphi 的函数覆盖(Override)与重载(overload
- 虚函数的使用 以及虚函数与重载的关系, 空虚函数的作用,纯虚函数->抽象类,基类虚析构函数使释放对象更彻底
- extern static 的作用和注意点 && c++如何实现函数的重载
- DELPHI语法基础学习笔记-Windows 句柄、回调函数、函数重载等
- C++ 和 Delphi 的函数覆盖(Override)与重载(overload)
- 从一个貌似重载失败的例子,看虚函数的作用
- 怎么重载OnEraseBkgnd()函数、与其作用
- [C/C++]函数重载、作用域、隐藏
- delphi 字符串比较函数
- inline函数定义放在头文件中的作用
- 如何实现JS函数的重载
- Linux内核中的jiffies及其作用介绍及jiffies等相关函数详解
- 操作符函数及重载(3)
- C++类中的静态成员变量和静态成员函数的作用
- Delphi 下的通配符查找函数
- PHP 函数header作用
- JavaScript 函数、作用域和继承
- C++类成员函数的重载、覆盖和隐藏区别?