zoukankan      html  css  js  c++  java
  • 方法重载

    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.
    
  • 相关阅读:
    noi.ac 集合
    NOI2019 SX 模拟赛 no.5
    带花树草解
    UR#13 SRAND
    【51nod1847】 奇怪的数学题
    ●POJ 3237 Tree
    ●BZOJ 2049 [Sdoi2008]Cave洞穴勘测
    ●BZOJ 2002 [Hnoi2010]Bounce 弹飞绵羊
    ●POJ 2983 Is the Information Reliable?
    ●POJ 3378 Crazy Thairs
  • 原文地址:https://www.cnblogs.com/del/p/983733.html
Copyright © 2011-2022 走看看