一, 示例一,理解对象的引用:我们要搞清楚进行对象赋值的两个不同概念,
其一,是使用赋值操作符(:=)将一个对象的引用赋值给一个对象变量,产生的效果是
这两个变量指向的是同一个对象。其二,使用Assign或AssignTo方法可以将对象属性进行复制,
得到两个状态完全一样的两个对象。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm2 = class(TForm)
BtnSet: TButton;
dlgFont1: TFontDialog;
mmo1: TMemo;
BtnUndo: TButton;
procedure BtnSetClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure BtnUndoClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
procedure TForm2.BtnSetClick(Sender: TObject);
begin
if dlgFont1.Execute then
mmo1.Font:=dlgFont1.Font;
end;
procedure TForm2.BtnUndoClick(Sender: TObject);
begin
mmo1.Font:=dlgFont1.Font; //字体不会还原,因为两者是同一个对象。
end;
procedure TForm2.FormCreate(Sender: TObject);
begin
mmo1.Lines.Add('一个可以任意设置字体和复原字体的例子');
dlgFont1.Font:=mmo1.Font;
//这里dlgFont1.Font只是复制了mmo1.font的对象引用 dlgFont1.Font ,mmo1.Font 指向的是同一个对象 ;
end;
end.
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm2 = class(TForm)
BtnSet: TButton;
dlgFont1: TFontDialog;
mmo1: TMemo;
BtnUndo: TButton;
procedure BtnSetClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure BtnUndoClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
procedure TForm2.BtnSetClick(Sender: TObject);
begin
if dlgFont1.Execute then
mmo1.Font:=dlgFont1.Font;
end;
procedure TForm2.BtnUndoClick(Sender: TObject);
begin
mmo1.Font:=dlgFont1.Font; //字体不会还原,因为两者是同一个对象。
end;
procedure TForm2.FormCreate(Sender: TObject);
begin
mmo1.Lines.Add('一个可以任意设置字体和复原字体的例子');
dlgFont1.Font:=mmo1.Font;
//这里dlgFont1.Font只是复制了mmo1.font的对象引用 dlgFont1.Font ,mmo1.Font 指向的是同一个对象 ;
end;
end.
示例二,应用了对象复制
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm2 = class(TForm)
BtnSet: TButton;
dlgFont1: TFontDialog;
mmo1: TMemo;
ForginalFont:TFont;
BtnUndo: TButton;
procedure BtnSetClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure BtnUndoClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
procedure TForm2.BtnSetClick(Sender: TObject);
begin
if dlgFont1.Execute then
mmo1.Font:=dlgFont1.Font; //dlgFont1的font对象属性发生改变之后,赋值给mmo1.font控制字体样式
end;
procedure TForm2.BtnUndoClick(Sender: TObject);
begin
mmo1.Font.Assign(ForginalFont); //将 ForginalFont 赋值给mmo1.Font
end;
procedure TForm2.FormCreate(Sender: TObject);
begin
mmo1.Lines.Add('一个可以任意设置字体和复原字体的例子');
ForginalFont:=TFont.Create; //这里我们创建了font对象ForginalFont来保存还原的字体属性,而不只是引用
ForginalFont.Assign(mmo1.Font); //将创建的font对象,复制给mmo1.font一份
end;
end.
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm2 = class(TForm)
BtnSet: TButton;
dlgFont1: TFontDialog;
mmo1: TMemo;
ForginalFont:TFont;
BtnUndo: TButton;
procedure BtnSetClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure BtnUndoClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
procedure TForm2.BtnSetClick(Sender: TObject);
begin
if dlgFont1.Execute then
mmo1.Font:=dlgFont1.Font; //dlgFont1的font对象属性发生改变之后,赋值给mmo1.font控制字体样式
end;
procedure TForm2.BtnUndoClick(Sender: TObject);
begin
mmo1.Font.Assign(ForginalFont); //将 ForginalFont 赋值给mmo1.Font
end;
procedure TForm2.FormCreate(Sender: TObject);
begin
mmo1.Lines.Add('一个可以任意设置字体和复原字体的例子');
ForginalFont:=TFont.Create; //这里我们创建了font对象ForginalFont来保存还原的字体属性,而不只是引用
ForginalFont.Assign(mmo1.Font); //将创建的font对象,复制给mmo1.font一份
end;
end.