2015-06-17 83 views
2

我仍然在這裏與德爾福幀的問題。 我想創建一個以管理不同的數據庫表使用不同類型的幀的應用,所以想了解如何執行這樣的任務我已經創建一個簡單的德爾福形式:德爾福通用框架

unit main; 

interface 

uses 
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, 
    System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, 
    Vcl.StdCtrls, Vcl.ExtCtrls, FramesManagement; 

type 
    TfrmMain = class(TForm) 
    pnlCommands: TPanel; 
    pnlFrames: TPanel; 
    btnFrame1: TButton; 
    btnFrame2: TButton; 
    procedure FormCreate(Sender: TObject); 
    procedure btnFrame1Click(Sender: TObject); 
    procedure btnFrame2Click(Sender: TObject); 
    private 
    FFrame: IFrameManagement; 
    public 
    { Public declarations } 
    end; 

var 
    frmMain: TfrmMain; 

implementation 

{$R *.dfm} 

uses Frame1, Frame2; 

procedure TfrmMain.FormCreate(Sender: TObject); 
begin 
    FFrame := TFramemanagement.Create; 
end; 

procedure TfrmMain.btnFrame1Click(Sender: TObject); 
begin 
    FFrame.CreateGenericFrame(pnlFrames, TFrame(Frame1.TFra1)); 
end; 

procedure TfrmMain.btnFrame2Click(Sender: TObject); 
begin 
    FFrame.CreateGenericFrame(pnlFrames, TFrame(Frame2.TFra2)); 
end; 

end. 

這表格使用以下聲明:

unit FramesManagement; 

interface 

uses 
    Vcl.Forms, Vcl.StdCtrls, Vcl.ExtCtrls, Frame1, Frame2; 

type 

    IFrameManagement = interface 
    ['{A00E0D1B-3438-4DC4-9794-702E8302B567}'] 
    procedure CreateGenericFrame(ParentPanel: TPanel; FrameName: TFrame); 
    end; 

    TFrameManagement = class(TInterfacedObject, IFrameManagement) 
    private 
    genericFrame: TFrame; 
    procedure CreateGenericFrame(ParentPanel: TPanel; FrameName: TFrame); 
    end; 

implementation 

procedure TFrameManagement.CreateGenericFrame(ParentPanel: TPanel; 
    FrameName: TFrame); 
begin 
    genericFrame := FrameName.Create(ParentPanel); 
    genericFrame.Parent := ParentPanel; 
end; 

而這裏有兩個框架。

1幀:

unit Frame1; 

interface 

uses 
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, 
    System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, 
    Vcl.StdCtrls; 

type 
    TFra1 = class(TFrame) 
    txtFrame1: TStaticText; 
    txtFrameType: TStaticText; 
    lblFrameType: TLabel; 
    private 

    public 

    end; 

implementation 

{$R *.dfm} 

end. 

和2幀:

unit Frame2; 

interface 

uses 
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, 
    System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, 
    Vcl.StdCtrls; 

type 
    TFra2 = class(TFrame) 
    txtFrame2: TStaticText; 
    txtFrameType: TStaticText; 
    lblFrameType: TLabel; 
    private 

    public 

    end; 

implementation 

{$R *.dfm} 

end. 

這是所有的代碼,但是當運行該應用程序和我嘗試創建第一或我接收第二幀這樣的錯誤:

enter image description here

我認爲SOLU這可能是泛型的使用,但我不知道如何使用它們。我的想法是對的還是有另一種方式來接收這個gol? 任何人都可以幫助我嗎?

+1

'Generics'在編程一個非常特定的含義,密切相關的C++模板。這不是一個非特定的:-)的正確標籤。 – Johan

+0

@Johan不錯,不過「通用」這個詞也是一個非常通用的術語:-)可以使用這個詞,但不能作爲標籤。 –

回答

7
procedure TFrameManagement.CreateGenericFrame(ParentPanel: TPanel; FrameName: TFrame); 
begin 
    genericFrame := FrameName.Create(ParentPanel); 
    genericFrame.Parent := ParentPanel; 
end; 

這裏FrameName是一個實例,您正在調用該實例的構造函數。你不打算創建一個新的實例。

您需要使用元類。

type 
    TFrameClass = class of TFrame; 

procedure TFrameManagement.CreateGenericFrame(ParentPanel: TPanel; FrameClass: TFrameClass); 
begin 
    genericFrame := FrameClass.Create(ParentPanel); 
    genericFrame.Parent := ParentPanel; 
end; 

您可以致電此像這樣:

FFrame.CreateGenericFrame(pnlFrames, Frame2.TFra2);