2015-08-16 49 views
1

我有一個項目和兩個單位和主程序 第一單元在這裏它的主要問題是這個類的構造函數:這段代碼中的構造函數不好。有人可以幫助我管理這些代碼嗎?

unit dl_tPA_MailJournal; 

interface 
uses 
    Windows, 
    Generics.Collections, 
    SysUtils, 
    uInterfaces; 
    type 
    TtPA_MailJournal = class(TInterfacedObject ,ITable) 
     public 
     function GetanQId: integer; 
     procedure SetanQId(const Value: integer); 
     function GetadDate: TDateTime; 
     procedure SetadDate(const Value: TDateTime); 

     function toList: TList<string>; 

     constructor Create(aId : Integer; aDate : TDateTime); 


     private 
     property anQId : integer read GetanQId write SetanQId; 
     property adDate : TDateTime read GetadDate write SetadDate; 


    end; 

implementation 

{ TtPA_MailJournal } 

constructor TtPA_MailJournal.Create(aId : Integer; aDate : TDateTime); 
begin 
    SetanQId(aId); 
    SetadDate(aDate); 
end; 

function TtPA_MailJournal.GetadDate: TDateTime; 
begin 
    Result := adDate; 
end; 

function TtPA_MailJournal.GetanQId: integer; 
begin 
    Result := anQId ; 
end; 

procedure TtPA_MailJournal.SetadDate(const Value: TDateTime); 
begin 
    adDate := Value; 
end; 

procedure TtPA_MailJournal.SetanQId(const Value: integer); 
begin 
    anQId := Value; 
end; 
function TtPA_MailJournal.toList: TList<string>; 
var aListTable: TList<TtPA_MailJournal>; 
var aTable: TtPA_MailJournal; 
var aListString: TList<String>; 
begin 
    aTable.Create(1,now); 
    aListTable.Add(aTable); 
    aTable.Create(2,now); 
    aListTable.Add(aTable); 
    aListString.Add(aListTable.ToString); 

    Result := aListString; 
end; 
end. 

第二單元是沒有多少在這裏看到

接口
unit uInterfaces; 

interface 
uses Generics.Collections; 
type 

    ITable = Interface 
    ['{6CED8DCE-9CC7-491F-8D93-996BE8E4D388}'] 
    function toList: TList<String>; 
    End; 

implementation 

end. 

的主類,在這裏我想StringList的仿製藥,並放入格:

unit MainUnit; 

interface 

uses 
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, 
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, 
    dl_tPA_MailJournal,uInterfaces, Vcl.StdCtrls, 
    Generics.Collections, Vcl.Grids; 

type 
    TForm1 = class(TForm) 
    Button1: TButton; 
    StringGrid1: TStringGrid; 
    procedure Button1Click(Sender: TObject); 

    private 
    { Private declarations } 
    public 
    { Public declarations } 
    end; 

var 
    Form1: TForm1; 


implementation 

{$R *.dfm} 

procedure TForm1.Button1Click(Sender: TObject); 
var MyTable: TtPA_MailJournal; 
    MyList: TList<String>; 
    AStringList: TStrings; 
    StrDate : string; 
    Fmt: TFormatSettings; 
begin 

    //fmt.ShortDateFormat:='dd/mm/yyyy'; 
    // fmt.DateSeparator :='/'; 
    // StrDate:='23/02/2011' ; 


    MyTable := TtPA_MailJournal.Create(1,now);  //strtodate(strdate,fmt) 

    MyList := MyTable.toList; 

    AStringList := TStringList.Create; 
    AStringList.Add(MyList.ToString); 
    StringGrid1.Cols[1].Add(MyList.ToString); 
    FreeAndNil(MyTable); 




end; 

end. 

當我點擊程序崩潰的按鈕。當我評論這兩個 行的構造函數SetanQId(aId);和SetadDate(aDate);這是好的 我做錯了什麼可以告訴我如何管理這個代碼,以顯示在網格請。

+0

您設定自己的二傳手屬性值依次調用一遍傳,等我 – TLama

+0

其更改爲援助:所以你的構造應進行編碼=援助; aDate:= aDate;現在我得到訪問衝突錯誤 – ververicka

+0

編程不是試錯過程。你嘗試過的改變基本上是一樣的。它會爲該屬性設置一個值,該屬性將調用setter方法,在該方法中,您將爲將調用setter的屬性設置一個值...等等。你已經結束了無限循環。我不知道這些屬性的目的是什麼,但是根據你的getter和setter做的事情,我會說你想要[將讀寫私人字段的屬性](http://pastebin.com/Rjve1Knp)。 – TLama

回答

2

聲明屬性不會爲該屬性分配任何存儲。它僅僅是一個指向存儲值的指針。儘管您可以聲明getter和setter函數來訪問屬性值,但最終屬性值必須由某個字段進行備份。

TtPA_MailJournal = class(TInterfacedObject ,ITable) 
public 
    FanId: integer; 
    FadDate: TDateTime; 
    function GetanQId: integer; 
    procedure SetanQId(const Value: integer); 
    function GetadDate: TDateTime; 
    procedure SetadDate(const Value: TDateTime); 

    function toList: TList<string>; 

    constructor Create(aId : Integer; aDate : TDateTime); 
private 
    property anQId : integer read GetanQId write SetanQId; 
    property adDate : TDateTime read GetadDate write SetadDate; 
end; 

function TtPA_MailJournal.GetadDate: TDateTime; 
begin 
    Result := FadDate; 
end; 

function TtPA_MailJournal.GetanQId: integer; 
begin 
    Result := FanQId ; 
end; 

procedure TtPA_MailJournal.SetadDate(const Value: TDateTime); 
begin 
    FadDate := Value; 
end; 

procedure TtPA_MailJournal.SetanQId(const Value: integer); 
begin 
    FanQId := Value; 
end; 

如果你沒有任何特別需要getter和setter方法你可以宣佈你的財產直接訪問其存儲領域:

property anQId : integer read FanQId write FanQId; 
    property adDate : TDateTime read FadDate write FadDate; 

此外,具有私人性質纔有意義,當你在getter和/或setter方法中實現了一些額外的邏輯。在這種特殊情況下,它們沒有多大意義,因此您可以完全刪除屬性聲明,並直接使用字段。

constructor TtPA_MailJournal.Create(aId : Integer; aDate : TDateTime); 
begin 
    inherited Create; 
    FanQId := aId; 
    FadDate := aDate; 
end; 

首先,你需要初始化結果和aListTable變量。那麼你正在使用錯誤的方式構造函數而不是aTable.Create(1, now)你應該使用aTable := TtPA_MailJournal.Create(1,now)

你在這裏的另一個問題是,你是混合對象和接口引用,從而泄漏對象。因此,您應該使用ITable來存儲對您的TtPA_MailJournal對象實例的引用。

function TtPA_MailJournal.toList: TList<string>; 
var 
    aListTable: TList<ITable>; 
    aTable: ITable; 
begin 
    Result := TList<String>.Create; 
    try 
    aListTable := TList<ITable>.Create; 
    aTable := TtPA_MailJournal.Create(1,now); 
    aListTable.Add(aTable); 

    aTable := TtPA_MailJournal.Create(2,now); 
    aListTable.Add(aTable); 
    Result.Add(aListTable.ToString); 
    finally 
    aListTable.Free; 
    end; 
end; 

你也應該釋放你的MyList對象,當你用它做,否則會出現內存泄漏。

+0

我得到山姆錯誤訪問衝突 – ververicka

+0

此外你的填充字符串網格的代碼可能是錯誤的,但由於它不清楚你想如何填充它,我會離開那部分讓你想出... –

+0

私有屬性使特別是當吸氣劑和製取者不僅僅是讀寫字段時。 –

1

您的堆棧空間不足。您可以通過調用

SetanQId(aId); 

然後包含語句設置屬性:

anQId := Value; 

現在,anQId是一個屬性,它已宣佈要調用SetanQId方法上的每個任務,所以在本質上上面的語句翻譯成

SetanQId(Value); 

其中調用SetanQId方法,該方法包含語句

現在
anQId := Value; 

,anQId是一個屬性,它已宣佈要調用SetanQId方法上的每個任務,所以在本質上面的語句被翻譯成

SetanQId(Value); 

其調用SetanQId方法,其中包含聲明

anQId := Value; 

現在,anQId是一個屬性,它已宣佈要調用SetanQId方法上的每個任務,所以在本質上面的語句被翻譯成

SetanQId(Value); 

(看到這是怎麼回事?)

您有吸氣劑同樣的問題 - 這也結束了自稱循環往復。

您需要聲明一個支持字段來保存屬性值,它是這個支持字段,您應該讀取和寫入屬性獲取器和設置器。

另外一件事:您應該將其作爲一種標準做法,始終從您自己的方式調用父級的Create方法。儘管在這個特定的實例中它可能並不重要,但是通常來說 - 不這樣做是一個壞主意,因爲父項的構造函數中可能需要運行代碼才能使對象正常工作。

constructor TtPA_MailJournal.Create(aId : Integer; aDate : TDateTime); 
begin 
    inherited Create; 
    SetanQId(aId); 
    SetadDate(aDate); 
end; 

constructor TtPA_MailJournal.Create(aId : Integer; aDate : TDateTime); 
begin 
    inherited Create; 
    anQId:=aId; 
    adDate:=aDate; 
end; 
+0

好吧,我會刪除。 – MartynA

相關問題