5,927
社区成员




TForm1 = class(TForm)
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; Override;
end;
。。。
var
frmA: TForm1;
begin
frmA := TForm1.create(nil); //怎样在创建完成后,在类内部的代码中,把frmA置为nil
。。。
end;
type
TMyObj = class(TObject)
private
FID: Integer;
public
constructor Create;
end;
{ TMyObj }
constructor TMyObj.Create;
{$J+}
const
SObj: TMyObj = nil;
{$J-}
begin
if SObj = nil then
begin
SObj := Self;
FID := 1;
end
else
begin
FID := 2;
asm
mov ebx, esi
test dl, dl
jz @@exit
mov ebx, esi
mov eax, ebx
mov edx, [eax]
call dword ptr [edx - $1c]
mov dl, $01
mov ecx,[eax]
call dword ptr [ecx-$04]
pop dword ptr fs:[$00000000]
add esp, $0c
mov esi, [SObj]
mov ebx,0
@@exit:
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
a: TMyObj;
begin
a := TMyObj.Create;
ShowMessage('单例-对象地址:' + IntToHex(Integer(a), 8));
end;
type
TMyObj = class(TObject)
public
constructor Create;
end;
{ TMyObj }
constructor TMyObj.Create;
asm
mov ebx, esi
test dl, dl
jz @@exit
mov ebx, esi
mov eax, ebx
mov edx, [eax]
call dword ptr [edx - $1c]
pop dword ptr fs:[$00000000]
add esp, $0c
mov esi, 0
mov ebx,0
@@exit:
end;
procedure TForm1.Button1Click(Sender: TObject);
var
a: TMyObj;
begin
a := nil;
a := TMyObj.Create;
if a <> nil then
a.Free
else
ShowMessage('空');
end;
class function TSingleton.NewInstance: TObject;
begin
if ( not Assigned( Instance ) ) then
begin
Instance := inherited NewInstance;
// Initialize private variables here, like this:
// TSingleton(Result).Variable := Value;
end;
Result := Instance
Inc( Ref_Count );
end;
function CreateObj(AOwner: TComponent): TObj;
begin
try
Result := TObj.Create(AOwner);
except
Result := nil;
end;
end;
constructor TObj.Create;
begin
Self := nil;
end;