方法重载


unit Unit1;



interface



uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs;



type

  TForm1 = class(TForm)

    procedure FormCreate(Sender: TObject);

  private

    { Private declarations }

  public

    { Public declarations }

  end;



//有方法重载的类

  TMyClass = class

  public

    procedure MyProc(i: Integer); overload;

    procedure MyProc(s: string); overload;

    function MyProc(s1,s2: string): string; overload;

  end;

//关于方法重载:

//过程和函数之间可以重载

//类内重载必须有 overload 关键字

//子类重载必须有 overload 关键字,夫类可以没有

//如果夫类是虚函数(virtual dynamic),子类重载时需要加 reintroduce 修饰词

//published 区内不能重载



var

  Form1: TForm1;



implementation



{$R *.dfm}



{ TMyClass }



procedure TMyClass.MyProc(i: Integer);

begin

  ShowMessage(IntToStr(i));

end;



procedure TMyClass.MyProc(s: string);

begin

  ShowMessage(s);

end;



function TMyClass.MyProc(s1, s2: string): string;

begin

  Result := s1 + ' and ' + s2;

end;



//测试

procedure TForm1.FormCreate(Sender: TObject);

var

  class1: TMyClass;

begin

  class1 := TMyClass.Create;



  class1.MyProc(2);  //2

  class1.MyProc('2');  //2

  ShowMessage(class1.MyProc('2','3'));  //2 and 3



  class1.Free;

end;



end.


 
   

你可能感兴趣的:(方法重载)