【发布时间】:2019-09-11 07:25:26
【问题描述】:
我写了这个简单的类:
unit Test;
interface
uses
SysUtils;
type
TGram = class
private
instanceCreated: boolean;
constructor Create;
public
procedure test;
end;
implementation
constructor TGram.Create;
begin
instanceCreated := true;
end;
procedure TGram.test;
begin
if not instanceCreated
then raise Exception.Create('The object has not been created.');
end;
end.
当我调用方法 test 时,我得到了它没有被创建的异常。
var test: TGram;
begin
test := TGram.create;
test.test;
end
在构造函数中,instanceCreated 设置为 true(我相信如此),但是当我稍后尝试访问它时,它不存在。为什么?如何纠正这个问题?
【问题讨论】:
-
你建议如何改变Q?
-
你确定需要 private 构造函数吗?
-
zed:谢谢!这就是答案!
标签: object delphi constructor