Delphi: Method 'Create' hides virtual method of base - but it's right there

后端 未结 2 1968
广开言路
广开言路 2020-12-06 16:01

Consider the hypothetical object hierarchy, starting with:

TFruit = class(TObject)
public
    constructor Create(Color: TColor); virtual;
end;
<
2条回答
  •  执念已碎
    2020-12-06 16:45

    This appears to be a "which came first" sort of issue. (It appears NGLN found a solution.)

    There's another solution, also. You can use a default parameter:

    interface
    
    type
      TFruit=class(TObject)
      public
        constructor Create(Color: TColor); virtual;
      end;
    
      TApple=class(TFruit)
      public
        constructor Create(Color: TColor = clRed); override;
      end;
    
    implementation
    
    { TFruit }
    
    constructor TFruit.Create(Color: TColor);
    begin
      inherited Create;
    end;
    
    { TApple }
    
    constructor TApple.Create(Color: TColor);
    begin
      inherited;
    end;
    
    // Test code
    var
      AppleOne, AppleTwo: TApple;
    begin
      AppleOne := TApple.Create;
      AppleTwo := TApple.Create(clGreen);
    end;
    

提交回复
热议问题