关于property的写方法?(50分)

  • 主题发起人 主题发起人 ff_ff
  • 开始时间 开始时间
F

ff_ff

Unregistered / Unconfirmed
GUEST, unregistred user!
组件设计时,如果某一属性为对象。
如:
type
TmyObj=class(Tpersistent)
private
F1:String;
procedure assign(value:Tobject);
end;
Tmycom=class(Tpersistent)
private:
f:TmyObj;
procedure setf(value:Tobject);
published:
property f1:TmyObj read f write setf;
end;
...
Tmycom.setf(value:Tobject);
begin
if assigned(value) then
f.assign(value);//时myobj的
end;
如过直接操作f
property f:TmyObj read f write f;会又什么后果?
为什么在读对象属性写//##为什么在写f1时要setf
 
property f1:TmyObj read f write f;只能把一个Tmyobj对象的指针赋予f,如果这个Tmyobj对象
在外部被销毁,则f指向的就不再是有效内存,只要引用f就会出错的。下面的例子你试试就清楚了。
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
Taaa = class
public
str:string;
end;
Tbbb = class
private
Ff:Taaa;
published
property f:Taaa read Ff write Ff;//会又什么后果?
end;
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
aaa:Taaa;
bbb:Tbbb;
begin
aaa:=Taaa.create;
aaa.str:='abcde';
bbb:=Tbbb.Create;
bbb.f:=aaa;
Freeandnil(aaa);
Edit1.Text:=bbb.f.str;
freeandnil(bbb);
Tpersistent
end;

end.
 
同意楼上说的,用 read 和 write 也可以,但是只是一个指针操作,不是真正意义上的数值的改变,所以在外部对象销毁的情况下会发生错误,使用write 方法,可以直接改变指针所指内存地址的具体内容,这样不容易出错!!
对于一些重要的属性不仅需要写write 过程 read的过程也是需要的
这个由控件的设计者自行来确定!!
 
谢谢二位:
to flamboyant:
to lxfsxxbbs-1:
bbb.f:=aaa;对f进行写操作。直接把aaa的地址付给Ff.
当aaa销毁后,bbb在引用f就会出错。
我想改进一下。
...
Tbbb = class
private
Ff:Taaa;
public
procedure setf (value:Taaa);
published
property f:Taaa read Ff write Ff;//会又什么后果?
end;
...
Tbbb.setf(value:Taaa);
begin
if assigned(value) then //如果value不为空
Ff:=value 把value付给F1;
end;

我再试试,

大家给我分析分析。谢谢
 
死机啦,快来看看那。
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
Taaa =class
str:String;
end;
Tbbb = class
private
Ff:Taaa;
procedure setf(value:Taaa);
published
property f:Taaa read Ff write setf;
end;
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
aaa:Taaa;
bbb:Tbbb;
begin
aaa:=Taaa.Create;
aaa.str:='hello';
bbb:=Tbbb.Create;

bbb.f:=aaa;
freeandnil(aaa);
Edit1.Text:=bbb.f.str;


end;

{ Tbbb }

procedure Tbbb.setf(value: Taaa);
begin
if assigned(value) then //这里会是死循环?
f:=value;//偶,这里又会调Setf

end;


end.
大ggjj来侃侃
 
我觉的
procedure Tbbb.setf(value: Taaa);
begin
f.assigned(value)
end;
就行了,
 
楼上的兄弟,你的assigned方法从哪里来的?
 
多人接受答案了。
 
这个可以,
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TSomeObject = class(TPersistent)
private
FProp1 : Integer;
FProp2 : String;
public
procedure Assisgn(Source : TPersistent);
published
property Prop1 : Integer read FProp1 write FProp1;
property Prop2 : String read FProp2 write FProp2;
end;
Tddg = class(TCustomControl)
private
FSomeObject : TSomeObject;
procedure SetSomeObject(Value : TSomeObject);
public
constructor Create(AOwner : TComponent);override;
destructor destroy;override;
published
property SomeObject : TSomeObject read FSomeObject write SetSomeObject;
end;

TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

{ TSomeObject }

procedure TSomeObject.Assisgn(Source: TPersistent);
begin
if Source is TSomeObject then
begin
FProp1 := TSomeObject(Source).Prop1;
FProp2 := TSomeObject(Source).Prop2;
inherited Assign(Source);
end;
end;

{ Tddg }

constructor Tddg.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FSomeObject := TSomeObject.Create;
end;

destructor Tddg.destroy;
begin
FSomeObject.Free;
inherited destroy;
end;

procedure Tddg.SetSomeObject(Value: TSomeObject);
begin
if Assigned(Value) then
FSomeObject.Assisgn(Value);
end;

procedure TForm1.Button1Click(Sender: TObject);
var Some1 : TSomeObject;
Some2 : Tddg;
begin
some1 := TSomeObject.Create;
some1.Prop1 := 123;
some1.Prop2 := '123456';
some2 := Tddg.Create(self);
some2.SomeObject.Prop2 := some1.Prop2;
freeandnil(some1);
Edit1.Text := some2.SomeObject.Prop2;
end;

end.
 
后退
顶部