..... TFriendClass = class of TFriend;....var clazz : TFriendClass; begin clazz := TFriend; clazz.newInstance; .... end; newInstance will only call default constructor. i.e. constructor without any arguments.Hope it helps.//Ali
关于上面的代码我有几点疑问,谢谢 1.为什么一定要用类类型? 为什么不能这样? var clazz : TFriend; begin clazz := TFriend.create;
If you have only one class then for sure you can use that in this way, but in case where you need to invoke different instances based on dynamic parameters then class of is your best friend. Confused!!!! What are you trying to achieve? You got conflict in your statement 1 and 2. From statement 2 look like you want to have invoke different class instances.Are you talking about naming variables?//Ali
你定义一个全局动态数组: var FFriends :array of TAFriend ;每添加一个好友就新建一个实例 ; var f :TAFriend ;begin SetLength(FFriends , Length(FFriends ) + 1); f := TAFriend.create() ; f.Name := ...//设置属性 FFriends[High(FFriends)] := f ;
遍历: var i: integer; f :TAFriend ; begin
for (i := 0 to High(FFriends)) do begin f := FFriends[i] ; end; end;
.....
TFriendClass = class of TFriend;....var
clazz : TFriendClass;
begin
clazz := TFriend;
clazz.newInstance;
....
end;
newInstance will only call default constructor. i.e. constructor without any arguments.Hope it helps.//Ali
关于上面的代码我有几点疑问,谢谢
1.为什么一定要用类类型?
为什么不能这样?
var
clazz : TFriend;
begin
clazz := TFriend.create;
....
end;2.我想让每个生成的实例动态命名,这怎么实现?
是否是这样?
clazz.name:=xxxxxx
If you have only one class then for sure you can use that in this way, but in case where you need to invoke different instances based on dynamic parameters then class of is your best friend.
Confused!!!! What are you trying to achieve? You got conflict in your statement 1 and 2. From statement 2 look like you want to have invoke different class instances.Are you talking about naming variables?//Ali
var
FFriends :array of TAFriend ;每添加一个好友就新建一个实例 ;
var
f :TAFriend ;begin
SetLength(FFriends , Length(FFriends ) + 1);
f := TAFriend.create() ;
f.Name := ...//设置属性
FFriends[High(FFriends)] := f ;
var
i: integer;
f :TAFriend ;
begin
for (i := 0 to High(FFriends)) do
begin
f := FFriends[i] ;
end;
end;
2.你要给对象明名,可能是为了区分对象,可以在对象里面加个属性就可以解决。
3.建议,可以建一个TFriends = tobjectlist来管理TFriend对象。