有两种记录类型:
TPoint=record
  Counter:Integer;
  Value:Byte;
end;TPeak=record
  Counter:Integer;
  Value:Byte;
  Width:Integer;
end;这两种记录类型的数据,都需要一个环行缓冲管理器来管理,该环行缓冲区管理器要实现的一些功能完全相同。为了减少代码重复(提高代码复用率并减少差错),应该只设计一个通用的环行缓冲区管理类,可是我不知道应该怎样来设计这个类,所以现在万不得已,只能编下面的重复代码,希望高手指点我怎样才能写出这个通用类:TRingBufferA=class(TObject)
private
  FBuffer:Array [0..1024] of TPoint;
  ...
public
  property Current:TPoint read GetCurrent;
  property Last:TPoint read GetLast;
  property Item[ItemIndex:Integer]:TPoint read GetItem;default;
  function Add(APoint:TPoint):Integer;
end;TRingBufferB=class(TObject)
private
  FBuffer:Array [0..1024] of TPeak;
  ...
public
  property Current:TPeak read GetCurrent;
  property Last:TPeak read GetLast;
  property Item[ItemIndex:Integer]:TPeak read GetItem;default;
  function Add(APeak:TPeak):Integer;
end;
附带还有另外一个问题也让我比较难受,下面的语句编译不通过:
RingBufferA[3].Value:=50;
编译器报告说左边不能被赋值,现在我变通的办法是,将类中的代码:
property Item[ItemIndex:Integer]:TPeak read GetItem;default;
改成:
property Item[ItemIndex:Integer]:TPeak read GetItem write SetItem;default
...
TRingBufferA.SetItem(ItemIndex: Integer; const APoint: TPoint);
begin
  Buffer[ItemIndex].Counter:=APoint.Counter;
  Buffer[ItemIndex].Value:=APoint.Value;
end;真的就不能直接赋值吗?谢谢!  

解决方案 »

  1.   

    unit uCircleMem;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, math, StdCtrls, Buttons, uImagePanel, ExtCtrls, Contnrs;Type
      TObjType=(otPoint,otPeak);
         TPoint=record
        Counter:Integer;
        Value:Byte;
      end;  TPeak=record
        Counter:Integer;
        Value:Byte;
        Width:Integer;
      end;  //组合对象
      TObj=class
        obj:TObject;
        objType:TObjType;
      end;
      //列表对象
      TObjList=class(TObjectList)
      private
        fObjMaxCount: integer;
        fItemIndex: Integer;
        function GetObjs(ItemIndex: Integer): TObj;
        procedure SetObjs(ItemIndex: Integer; const Value: TObj);
        procedure setItemIndex(const Value: Integer);
      public
        constructor Create;
        property Objs[ItemIndex:Integer]:TObj read GetObjs write SetObjs;
        property ObjMaxCount:integer read fObjMaxCount write fObjMaxCount;    property ItemIndex:Integer read fItemIndex write setItemIndex;    function Add(AObject: TObject): Integer;
        function First: TObject;
        function Last: TObject;
        function GetObjAt(ItemIndex:integer):TObject;
        function Next:TObject;
        function Prev:TObject;
      end;implementation{ TObjList }function TObjList.Add(AObject: TObject): Integer;
    begin
      if Count < fObjMaxCount then
        Inherited Add(AObject);
    end;constructor TObjList.Create;
    begin
      fObjMaxCount := 0;
    end;function TObjList.First: TObject;
    begin
      Result := TObject(inherited First);
      fItemIndex := 0;
    end;function TObjList.GetObjAt(ItemIndex: integer): TObject;
    begin
      Result := nil;
      if ItemIndex in [0..count-1] then
      begin
        result := Objs[ItemIndex];
        fItemIndex := ItemIndex;
      end;
    end;function TObjList.GetObjs(ItemIndex: Integer): TObj;
    begin
      result := inherited Items[ItemIndex] as TObj;
    end;function TObjList.Last: TObject;
    begin
      Result := TObject(inherited Last);
      fItemIndex := count -1;
    end;function TObjList.Next: TObject;
    begin
      Result := nil;
      if ItemIndex in [0..count-2] then
      begin
        result := objs[ItemIndex+1];
        inc(fItemIndex);
      end;
    end;function TObjList.Prev: TObject;
    begin
      Result := nil;
      if ItemIndex in [1..count-1] then
      begin
        result := objs[ItemIndex-1];
        dec(fItemIndex);
      end;
    end;procedure TObjList.setItemIndex(const Value: Integer);
    begin
      fItemIndex := min(max(0,Value),count-1);
    end;procedure TObjList.SetObjs(ItemIndex: Integer; const Value: TObj);
    begin
      inherited Items[ItemIndex] := Value;
    end;end.
      

  2.   

    使用 TObjList 管理 Tobj,TObj可以兼容两中记录类对象