|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- unit Unit1;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, StdCtrls;
- type
- TForm1 = class(TForm)
- edt1: TEdit;
- edt2: TEdit;
- Button1: TButton;
- procedure Button1Click(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- Form1: TForm1;
- type
- Tinfo = class(TObject)
- public
- function showInfo(var str: string): string; overload; virtual;
- {重载 ,避免同名报错 reintroduce 进行隐匿 Tinfo的同名函数}
- function showInfo(var num: integer): string; reintroduce; overload;
- end;
- implementation
- var
- info: Tinfo;
- {$R *.dfm}
- procedure TForm1.Button1Click(Sender: TObject);
- begin
- info := Tinfo.Create; //实例化
- edt1.Text := info.showInfo('这是字符串');
- edt2.Text := info.showInfo(7);
- info.Free;
- end;
- function Tinfo.showInfo(var str: string): string;
- begin
- Result := str;
- end;
- function Tinfo.showInfo(var num: integer): string;
- begin
- Result := IntToStr(num);
- end;
- end.
复制代码
//报错
[Pascal Error] Unit1.pas(41): E2250 There is no overloaded version of 'showInfo' that can be called with these arguments
[Pascal Error] Unit1.pas(42): E2250 There is no overloaded version of 'showInfo' that can be called with these arguments
[Pascal Fatal Error] Project1.dpr(5): F2063 Could not compile used unit 'Unit1.pas'
但是我明明就在函数声明的时候加了overload,为啥还是报错~~~~???
方法一:
function showInfo(str: string): string; overload; virtual;
{重载 ,避免同名报错 reintroduce 进行隐匿 Tinfo的同名函数}
function showInfo(num: integer): string; reintroduce; overload;
edt1.Text := info.showInfo('这是字符串');
edt2.Text := info.showInfo(7);
方法二:
形参 var str 表示传的是地址,在函数里面可以直接修改 button代码块中的 str,函数中又result:= str,
太多人余了不是????
形参加var 实参必须就是个变量名,是个变量
function showInfo(var str: string): string; overload; virtual;
{重载 ,避免同名报错 reintroduce 进行隐匿 Tinfo的同名函数}
function showInfo(var num: integer): string; reintroduce; overload;
procedure TForm1.btn1Click(Sender: TObject);
var
s:string;
i:Integer;
begin
s:='这是字符串';
i:=7;
info := Tinfo.Create; //实例化
edt1.Text := info.showInfo(s);
edt2.Text := info.showInfo(i);
info.Free;
end;
|
|