Indication of internship inheritance



  • There's a code of the folder and file class (that is, we need to distinguish them), inherited from TComponent, the code of the folder. How to implement the establishment of the relevant heritage objects (the test of succession), i.e., for each facility, to indicate the owner in the Create designer, which in the long term will provide the possibility of a series of descendants using the GetChildren procedure. The code contains a set of TComponent objects, so it is necessary to recall the parents ' indexes (Owner) and descendants for inheritance. The code is attached below.

    unit Unit1;
    

    interface

    uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs;

    type
    TForm1 = class(TForm)
    private
    { Private declarations }
    public
    { Public declarations }
    end;
    Tfolder=class(TComponent)
    private
    { Private declarations }
    FPath : string;
    FName : string;
    FComponent : TComponent;
    public
    { Public declarations }
    procedure SetFName(var Name: string);
    function GetFName : string;
    procedure SetFPath(var Path: string);
    function GetFPath : string;
    protected
    { Protected declarations }
    procedure GetChildren(Proc: TGetChildProc; Root: TComponent); override;
    procedure SetParentComponent (Value: TComponent); override;
    published
    { Published declarations }
    property Path : string read FPath write FPath;
    property Name : string read FName write FName;
    end;
    TFile=class(TComponent)
    private
    { Private declarations }
    FPath : string;
    FName : string;
    FSize : integer;
    public
    { Public declarations }
    procedure SetFName(var Name: string);
    function GetFName : string;
    procedure SetFPath(var Path: string);
    function GetFPath : string;
    procedure SetFSize(var Size: integer);
    function GetFSize : integer;
    protected
    { Protected declarations }
    procedure GetChildren(Proc: TGetChildProc; Root: TComponent); override;
    procedure SetParentComponent (Value: TComponent); override;
    published
    { Published declarations }
    property Path : string read FPath write FPath;
    property Name : string read FName write FName;
    property Size : integer read FSize write Fsize;
    end;
    var
    Form1: TForm1;
    ArrTfolder[200] : Tfolder;//массивы создаваемых объектов
    ArrTfile[200] : Tfile;

    implementation

    {$R *.dfm}

    procedure Tfolder.SetFName(var Name : string);
    begin
    FName:=Name;
    end;

    function Tfolder.GetFName : string;
    begin
    Result:=FName;
    end;

    procedure Tfolder.SetFPath(var Path : string);
    begin
    FPath:=Path;
    end;

    function Tfolder.GetFPath : string;
    begin
    Result:=FPath;
    end;
    procedure TFile.SetFName(var Name : string);
    begin
    FName:=Name;
    end;

    function TFile.GetFName : string;
    begin
    Result:=FName;
    end;

    procedure TFile.SetFPath(var Path : string);
    begin
    FPath:=Path;
    end;

    function TFile.GetFPath : string;
    begin
    Result:=FPath;
    end;

    procedure TFile.SetFSize(var Size : integer);
    begin
    FSize:=Size;
    end;

    function TFile.GetFSize : integer;
    begin
    Result:=FSize;
    end;

    procedure Tfolder.GetChildren(Proc: TGetChildProc; Root: TComponent);
    var I: integer;
    OwnedComponent : TComponent;
    begin
    inherited GetChildren(Proc, Root);
    if Root = Self then
    for I := 0 to ComponentCount - 1 do
    begin
    OwnedComponent := Components[I];
    if not OwnedComponent.HasParent then Proc(OwnedComponent);
    end;
    end;

    procedure Tfolder.SetParentComponent (Value: TComponent);
    begin

    end;

    procedure TFile.GetChildren(Proc: TGetChildProc; Root: TComponent);
    var I: integer;
    OwnedComponent : TComponent;
    begin
    inherited GetChildren(Proc, Root);
    if Root = Self then
    for I := 0 to ComponentCount - 1 do
    begin
    OwnedComponent := Components[I];
    if not OwnedComponent.HasParent then Proc(OwnedComponent);
    end;
    end;

    procedure TFile.SetParentComponent (Value: TComponent);
    begin

    end;

    procedure FindFiles(const DirPath: string; Str: TStrings);
    var
    SR: TSearchRec;
    begin
    if FindFirst(DirPath + '*.*', faAnyFile, SR) = 0 then
    try
    repeat
    if not ((SR.Name = '.') or (SR.Name = '..')) then
    begin
    if SR.Attr = faDirectory then
    FindFiles(DirPath + '' + SR.Name, Str)
    else
    Str.Add(SR.Name);
    end;
    until FindNext(SR) <> 0;
    finally
    FindClose(SR);
    end;
    end;
    end.



  • Reduce beyond:

    TFile=class(TComponent)  
    private  
      FPath  : String;  
      FSize  : Int64;  
      FParent: TFile;
    published  
      property Parent: TFile read FParent write FParent;
      property Path  : String read FPath write FPath;
      property Size  : Int64 read FSize write Fsize;  
    end;
    

    procedure FindFiles(const DirPath: string; parent: TFile; Owner: TComponent);
    var
    SR: TSearchRec;
    mfile: TFile;
    begin
    if FindFirst(DirPath + '*.*', faAnyFile, SR) = 0 then
    try
    repeat
    if not ((SR.Name = '.') or (SR.Name = '..')) then
    begin
    mfile:=TFile.Create(Owner);
    mfile.Path:=DirPath+ '' + SR.Name;
    mfile.Size:=SR.Size;
    mfile.Parent:=parent;//Это новое свойство в TFile
    if SR.Attr = faDirectory then
    FindFiles(DirPath + '' + SR.Name, mfile, Owner);
    end;
    until FindNext(SR) <> 0;
    finally
    FindClose(SR);
    end;
    end;

    procedure TForm1.MagicButtonClick(Sender: TObject);
    begin
    FindFiles('D:',nil,Self);
    end;

    So the files turned into components. Why do you need that?




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2