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

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

    Another possibility is to write a new constructor with default parameter values where the part of the signature with non-default parameters matches the original constructor in the base class:

    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; AEncoding: TEncoding = encDefault); reintroduce; virtual;
      end;
    
    constructor TXmlStream.Create(const AFileName: string);
    begin
      inherited Create;
      FFileName := AFileName;
    end;
    
    constructor TXhtmlStream.Create(const AFileName: string; AEncoding: TEncoding);
    begin
      inherited Create(AFileName);
      FEncoding := AEncoding;
    end;
    

提交回复
热议问题