2015-08-16 37 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

相关问题