例如:表中一个字段名是color,共有10条记录:red,yellow,red,green,brown,yello,red,black,white,yello请教:怎样用集合表示?谢谢!

解决方案 »

  1.   

    ......定义通用集合接口(里面的方法和属性可以根据你的需要进行增减)
      IListModel = interface
      ['{CA952A24-7AE9-4E56-832F-0149C6E44468}']
        procedure Add(const Item: IInterface);
        procedure Clear;
        function GetCount: Integer;
        function GetItem(Idx: Integer): IInterface;
        function IndexOf(const Item: IInterface): Integer;
        procedure Insert(const Item, Before: IInterface);
        procedure Move(const Item, Before: IInterface);
        procedure Remove(const Item: IInterface);
        function GetText: string;
        property Count: Integer read GetCount;
        property Item[Idx: Integer]: IInterface read GetItem;
        property Text: string read GetText;
      end;.....定义迭代子接口(这个不一定需要, 我就不公布了).....通用集合接口的实现  TListModel = class(TInterfacedObject, IListModel)
      private
        FItems: IInterfaceList;
        FText: string;
      protected
        property Items: IInterfaceList read FItems;
        property Text: string read FText write FText;
        //IListModel
        procedure Add(const Item: IInterface);
        procedure Clear;
        function GetCount: Integer;
        function GetItem(Idx: Integer): IInterface;
        function IndexOf(const Item: IInterface): Integer;
        procedure Insert(const Item, Before: IInterface);
        procedure Move(const Item, Before: IInterface);
        procedure Remove(const Item: IInterface);
        function GetText: string;
      public
        destructor Destroy; override;
      end;......定义元素类型  _TColor = (red,yellow,green,brown,yello,black,white,......); //这里的定义方法就多了......元素类型接口化  _TColorList =   _IColor = interface
      ['{691A7825-E33B-483C-BD71-D8E727C69773}']
        function GetColor: _TColor;
        procedure SetColor(const Value: _TColor);
        property Value: _TColor read GetColor write SetColor;
      end;......具体集合接口(派生于通用集合接口)  _IColorListModel = interface(IListModel)
      ['{E5AA99A7-1C37-4710-A0A2-701F5D73B5C6}']
        function GetItem(Idx: Integer): _IColor;
        property Item[Idx: Integer]: _IColor read GetItem;
      end;......具体集合接口的实现  _TColorListModel = class(TListModel, _IColorListModel)
      private
      protected
        //IPowers
        function _IColorListModel.GetItem = _ColorListModelGetItem;
        function _ColorListModelGetItem(Idx: integer): _IColor;
      public
      end;......实际应用就你的例子:字段Color这样来定义:
       ColumnColor: _IColorListModel;
    实例化:
       ColumnColor = _TColorListModel.Create;具体某一个颜色值(索引号Idx):
       ColumnColor[Idx].Value..........  
      
      

  2.   

    上面是我的实际应用中的一个对象模型根据你的COLOR略加修改而来的。