Delphi의 자동 방출 정책
//uses Vcl.StdCtrls, Vcl.ExtCtrls;
var
panel: TPanel;
procedure TForm1.Button1Click(Sender: TObject);
begin
panel := TPanel.Create(Self);
panel.Parent := Self;
with TButton.Create(panel) do //AOwner = panel
begin
Parent := panel;
Caption := 'New Button';
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if Assigned(panel) then
FreeAndNil(panel); // Owner
end;
2. 인터페이스 사용:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
end;
//
IBass = Interface
function GetName: string;
procedure SetName(const AName: string);
property Name: string read GetName write SetName;
end;
//
TBass = class(TInterfacedObject, IBass)
private
FName: string;
protected
function GetName: string;
procedure SetName(const AName: string);
public
constructor Create(const AName: string);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
X: IBass;
begin
X := TBass.Create('WanYi');
ShowMessage(X.Name);
X.Name := ' ';
ShowMessage(X.Name);
{X }
end;
{ TBass }
constructor TBass.Create(const AName: string);
begin
FName := AName;
end;
function TBass.GetName: string;
begin
Result := FName;
end;
procedure TBass.SetName(const AName: string);
begin
FName := AName;
end;
end.
3. 구조 사용:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
end;
//
TBass = record
private
FName: string;
procedure SetName(const AName: string);
public
constructor Create(const AName: string);
property Name: string read FName write SetName;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
X: TBass;
begin
X := TBass.Create('WanYi');
ShowMessage(X.Name);
X.Name := ' ';
ShowMessage(X.Name);
{X }
end;
{ TBass }
constructor TBass.Create(const AName: string);
begin
FName := AName;
end;
procedure TBass.SetName(const AName: string);
begin
FName := AName;
end;
end.
4. initialization에서 만들고finalization에서 놓기:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
var
List: TStringList;
procedure TForm1.Button1Click(Sender: TObject);
begin
List.Clear;
List.Add('WanYi');
ShowMessage(List.Text);
end;
initialization
List := TStringList.Create;
finalization
List.Free;
end.
5. Tlist, TstringList 등이 아닌 동적 배열을 사용합니다.
procedure TForm1.Button1Click(Sender: TObject);
var
arr: Array of string;
i: Integer;
s: string;
begin
for i := 0 to 6 do
begin
SetLength(arr, Length(arr)+1);
arr[High(arr)] := StringOfChar(Char(i+65), 3);
end;
for s in arr do ShowMessage(s);
end;
procedure TForm1.Button2Click(Sender: TObject);
var
arr: TArray<string>;
i: Integer;
s: string;
begin
for i := 0 to 6 do
begin
SetLength(arr, Length(arr)+1);
arr[High(arr)] := StringOfChar(Char(i+65), 3);
end;
for s in arr do ShowMessage(s);
end;
6. Tlist 대신 TObjectList를 사용합니다.
uses System.Contnrs;
procedure TForm1.Button1Click(Sender: TObject);
var
list: TObjectList;
i: Integer;
btn: TButton;
begin
list := TObjectList.Create;
for i := 0 to 6 do
begin
btn := TButton.Create(Self);
with btn do begin
Caption := Format('Btn %d', [i+1]);
Parent := Self;
Top := Height * i;
Left := Width * i div 2;
end;
list.Add(btn);
end;
ShowMessage('TObjectList , ');
list.Free;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
list: TList;
i: Integer;
btn: TButton;
begin
list := TList.Create;
for i := 0 to 6 do
begin
btn := TButton.Create(Self);
with btn do begin
Caption := Format('Btn %d', [i+1]);
Parent := Self;
Top := Height * i;
Left := Width * i div 2;
end;
list.Add(btn);
end;
ShowMessage('TList , ');
list.Free;
end;
7. Tlist 대신 TObjectList를 사용합니다.
uses System.Generics.Collections;
procedure TForm1.Button1Click(Sender: TObject);
var
list: TObjectList<TButton>;
i: Integer;
btn: TButton;
begin
list := TObjectList<TButton>.Create;
for i := 0 to 6 do
begin
btn := TButton.Create(Self);
with btn do begin
Caption := Format('Btn %d', [i+1]);
Parent := Self;
Top := Height * i;
Left := Width * i div 2;
end;
list.Add(btn);
end;
ShowMessage('TObjectList , ');
list.Free;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
list: TList<TButton>;
i: Integer;
btn: TButton;
begin
list := TList<TButton>.Create;
for i := 0 to 6 do
begin
btn := TButton.Create(Self);
with btn do begin
Caption := Format('Btn %d', [i+1]);
Parent := Self;
Top := Height * i;
Left := Width * i div 2;
end;
list.Add(btn);
end;
ShowMessage('TList , ');
list.Free;
end;
8. 구조체 포인터 대신 구조체를 사용한다.
{ }
function Area(rect: PRect): Integer;
begin
Result := rect.Width * rect.Height;
// Result := rect^.Width * rect^.Height;
end;
{ }
procedure TForm1.Button1Click(Sender: TObject);
var
P: PRect;
begin
New(P);
P^ := Rect(10, 10, 60, 50);
ShowMessage(IntToStr(Area(P))); //2000
Dispose(P);
end;
{}
procedure TForm1.Button2Click(Sender: TObject);
var
R: TRect;
begin
R := Rect(10, 10, 60, 50);
ShowMessage(IntToStr(Area(@R))); //2000
end;
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
양식 제출 후 제출 버튼 비활성화텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.