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

后端 未结 4 2160
深忆病人
深忆病人 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:28

    You could create two new overloaded constructors, for example:

    type
      TXmlStream = class
      private
        FFileName: string;
      public
        constructor Create(const AFileName: string); virtual;
      end;
    
      TXhtmlStream = class(TXmlStream)
      private
        FEncoding: TEncoding;
      public
        constructor Create(const AFileName: string); overload; override;
        constructor Create(const AFileName: string; AEncoding: TEncoding); overload; virtual;
      end;
    
    constructor TXmlStream.Create(const AFileName: string);
    begin
      inherited Create;
      FFileName := AFileName;
    end;
    
    constructor TXhtmlStream.Create(const AFileName: string);
    begin
      inherited Create(AFileName);
    end;
    
    constructor TXhtmlStream.Create(const AFileName: string; AEncoding: TEncoding);
    begin
      inherited Create(AFileName);
      FEncoding := AEncoding;
    end;
    

提交回复
热议问题