//很多VCL类中有item属性,就是使用了集合类
//第一个子条目类,实例中通常用数组访问内容。
class TMyCollectionItem :public TCollectionItem
{
public:
AnsiString MessageStr;
int MessageNo;
bool dothing;
__fastcall TMyCollectionItem(TCollection* Collection):TCollectionItem(Collection){};
};
//第二个类,管理类
class TMyCollection :public TCollection
{
private:
TMyCollectionItem * GetItem(int index);//子条目类
public:
__fastcall TMyCollection(TMetaClass * a):TCollection(a){}
TMyCollectionItem * Add();
__property TMyCollectionItem * Item[int]={read = GetItem};
};
TMyCollectionItem * TMyCollection::Add()//重载方法
{
TMyCollectionItem * m_collect = new TMyCollectionItem(this);
return m_collect;
}
TMyCollectionItem * TMyCollection::GetItem(int index)
{
TMyCollectionItem * re = (TMyCollectionItem *)Items[index];
return re;
}
void __fastcall TForm1::Button4Click(TObject *Sender)
{
TMyCollection * p = new TMyCollection(__classid(TMyCollectionItem));
TMyCollectionItem * a = p->Add();
a->MessageStr = "hello world";
a->MessageNo = 2000;
a->dothing = true;
TMyCollectionItem * c= p->Item[0];
ShowMessage(c->MessageStr);
}
//---------------------------------------------------------------------------
TCollection容器使用
最新推荐文章于 2019-06-24 16:58:50 发布