Delphi: How to add a different constructor to a descendant?

后端 未结 4 2138
深忆病人
深忆病人 2020-12-16 22:32

Update: The example i originally had was kind of complex. Here\'s a simple 8 line example that explains everything in one code block. The following

4条回答
  •  独厮守ぢ
    2020-12-16 23:18

    Also remember that constructors don't HAVE to be called Create. Older versions of Delphi didn't have method overloading, so you had to use different names:

    TComputer = class(TObject) 
    public 
        constructor Create(Cup: Integer); virtual; 
    end; 
    
    TCellPhone = class(TComputer) 
    private
      FTeapot: string;
    public 
        constructor CreateWithTeapot(Cup: Integer; Teapot: string); virtual; 
    end; 
    

    ...

    constructor TCellPhone.CreateWithTeapot(Cup: Integer; Teapot: string); 
    begin
      Create(Cup);
      FTeapot := Teapot;
    end;
    

    Both constructors will now be available.

提交回复
热议问题