Replace Delphi subclass constructor

I have the following classes declared in a single file:

type
 TCongruence = class(TObject)
  private
   a, b, n, numClass: integer;
   solutions, vals: TSolutions;
   hasSolutions: boolean;
   function divides(a, b: integer): boolean;
   function modulo(a, b: integer): integer;
  public
   constructor Create(a, b, n: integer); virtual;
   function getSolutions: TSolutions; virtual;
   function gcdExtended(p, q: integer): TSolutions;
   class function getGCD(u, v: integer): integer;
   property valA: integer read a;
   property valB: integer read b;
   property valN: integer read n;
   property getClass: integer read numClass;
   property hasSol: boolean read hasSolutions;
 end;

type
 TConguenceSystem = class(TCongruence)
  private
   system: array of TCongruence;
  public
   constructor Create(a: array of TCongruence); override;
   function getSolutions: integer; override;
 end;

The second one you see is a subclass, because I need to use all the functions implemented in the class TCongruence. I declared the constructor virtual so that I could call an override on the descendant.

It is right? Should I remove virtual / override and just use constructor like this? (Below)

constructor Create(a: array of TCongruence); 

I assume that in this case I am hiding my father’s constructor. I declared this constructor:

constructor TConguenceSystem.Create(a: array of TCongruence);
var i: integer;
begin

 SetLength(system, length(a)); // private var system: array of TCongruence
 for i := Low(a) to High(a) do
  begin
   system[i] := a[i];
  end;

 solutions := false;

end;
+4
source share
1 answer

, virtual , override.

, overload, . , , , . :

 TCongruence = class(TObject)
   public
     constructor Create(a : integer); overload;
     constructor Create(a, b, n: integer); overload;
 end;

, , , .

 TCongruence = class(TObject)
   public
     constructor Create(a, b, n: integer);
 end;

 TCongruenceSystem = class(TCongruence)
   public
     constructor Create(a: array of TCongruence);
 end;

- , . , , overload . :

 TCongruence = class(TObject)
   private
     Fa, Fb, Fn : integer;
   public
     constructor Create(a, b, n: integer);
 end;

 TCongruenceSystem = class(TCongruence)
   private
     FArr : array of TCongruence;
   public
     constructor Create(a: array of TCongruence);
 end;

constructor TCongruence.Create(a, b, n: integer);
begin
  inherited Create;
  Fa := a;
  Fb := b;
  Fn := n;
end;

constructor TCongruenceSystem.Create(a: array of TCongruence);
var
  c : TCongruence;
  i : integer;
begin
  inherited Create(a[0].Fa, a[1].Fb, a[2].Fn);
  SetLength(FArr, Length(a));
  i := 0;
  for c in a do begin
    FArr[i] := c;
    Inc(i);
  end;
end;

overload, , :

var
  cs : TCongruenceSystem;
begin
  cs := TCongruenceSystem.Create(1, 2, 3); 
end.

TCongruenceSystem Create, integer. overload, :

 TCongruence = class(TObject)
   private
     Fa, Fb, Fn : integer;
   public
     constructor Create(a, b, n: integer); overload;
 end;

 TCongruenceSystem = class(TCongruence)
   private
     FArr : array of TCongruence;
   public
     constructor Create(a: array of TCongruence); overload;
 end;

cs := TCongruenceSystem.Create(1, 2, 3); , .

, :

 TCongruence = class(TObject)
   public
     constructor Create(a : integer); overload; virtual; {overridable}
     constructor Create(a, b, n: integer); overload;     {only in base}
 end;

 TCongruenceSystem = class(TCongruence)
   public
     constructor Create(a:integer); overload; override;  {overrides parent}
     constructor Create(a: string); overload;            {introduce new}
 end;

, . getSolutions . , . getSolutions , .

+6

Source: https://habr.com/ru/post/1675037/


All Articles