在delphi中在子类构造方法中怎么调用父类的构造方法。
有没有类似CALL ,SUPPER之类的关健字可用?

解决方案 »

  1.   

    继承的时候,覆盖构造方法
    在子类的构造方法中加inherted
    不知道这样行不?
      

  2.   

    TParent = class(TObject)
        constructor Create;
      end;  TChild = class(TParent)
        constructor Create;
      end;
    implementation{ TParent }constructor TParent.Create;
    begin
      ShowMessage('TParent 创建对象')
    end;{ TChild }constructor TChild.Create;
    begin
      inherited;
      Showmessage('TChild 创建对象')
    end;
      

  3.   

    还是不行,我的父类和子类的构造方法全是有参数的(如下)。而且参数个数还不一样的。怎么办?
    ----------------------------------------------------------------------------------------
    TParent = class(TObject)
        constructor Create(参数一,参数二,参数三);//******************************
      end;  TChild = class(TParent)
        constructor Create(参数一,参数二,参数三,参数四);//******************************
      end;
    implementation{ TParent }constructor TParent.Create;
    begin
      ShowMessage('TParent 创建对象')
    end;{ TChild }constructor TChild.Create;
    begin
      inherited;
      Showmessage('TChild 创建对象')
    end;
      

  4.   

    type
      IMoveTo = interface(IUnKnown)
        procedure Move(Target: string);
      end;  TParent = class(TObject)
        constructor Create(a: string);
      end;  TChild = class(TParent)
        constructor Create(a: string; b: string);
      end;implementationconstructor TParent.Create(a: string);
    begin
      ShowMessage('P: ' + a);
    end;constructor TChild.Create(a: string; b: string);
    begin
      Inherited Create(a);
      ShowMessage('C: ' + b);
    end;end.
      

  5.   

    delphi在子类调用父类方法上做得非常好,一个inherited语句就OK了,
    不过要记住的是子类的构造函数一定要记得加上inherited,因为delphi的类构造函数是不会自动去调用父类的构造函数。
      

  6.   

    直接用inherited,不过我觉得这句其实就是等于父类同名方法里的整个代码
      

  7.   

    如果子类要重载父类的构造函数,而父类是虚构造函数的话,要加上Reintroduce。