程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 雙對象列表,雙字符串列表

雙對象列表,雙字符串列表

編輯:Delphi
 //在平時的應用中,經常會碰到一些,對象集TAs中每個A對象都對應每個B對象,對象TBs中每個對象B都對應一個A對象.  如果在A和B中各放一個Tobject屬性來進行互相引用,也是非常不錯的,但那樣依賴關系太強了.
  //同樣,我們經常會碰到一些這樣的情況,有一串的編號和一串的名稱進行對應,我們經常要從編號找名稱,或從名稱找編號.
  //所以,此處提取出來了兩個對象TDoubleObjList和TDoubleString,雖然可以從它們上面抽象出一個基類,但由於在Delphi中string的內存管理機制有點別扭,不屬於強類型, 所以還是分開兩個對象了.
  完整代碼如下:
  unit DoubleObject;

  interface
  uses
    Contnrs, Classes;

  type
    TDoubleObjList = class
    private
      ObjectList1: TObjectList;
      ObjectList2: TObjectList;
      FOwnsObjects: Boolean;
      procedure SetOwnsObjects(const Value: Boolean);
    public
      constructor Create;
      destructor Destroy; override;
      procedure Clear;
      property OwnsObjects: Boolean read FOwnsObjects write SetOwnsObjects;
      procedure AddData(AObject1, AObject2: TObject);

      function IndexOfData1(AIndex: Integer): TObject; overload;
      function IndexOfData1(AObject: TObject): Integer; overload;

      function IndexOfData2(AIndex: Integer): TObject; overload;
      function IndexOfData2(AObject: TObject): Integer; overload;

      function Data2GetData1(AObject: TObject): TObject;
      function Data1GetData2(AObject: TObject): TObject;

      function Count: Integer;
    end;

    TDoubleString = class
    private
      Strings1: TStringList;
      Strings2: TStringList;
    public
      constructor Create;
      destructor Destroy; override;
      procedure Clear;
      procedure AddString(AString1, AString2: string);

      function IndexOfData1(AIndex: Integer): string; overload;
      function IndexOfData1(AString: string): Integer; overload;

      function IndexOfData2(AIndex: Integer): string; overload;
      function IndexOfData2(AString: string): Integer; overload;

      function Data2GetData1(AString: string): string;
      function Data1GetData2(AString: string): string;

      function Count: Integer;
    end;
  implementation

  { TDoubleObjList }

  procedure TDoubleObjList.AddData(AObject1, AObject2: TObject);
  begin
    ObjectList1.Add(AObject1);
    ObjectList2.Add(AObject2);
  end;

  procedure TDoubleObjList.Clear;
  begin
    ObjectList1.Clear;
    ObjectList2.Clear;
  end;

  function TDoubleObjList.Count: Integer;
  begin
    Result := ObjectList1.Count;
  end;

  constructor TDoubleObjList.Create;
  begin
    FOwnsObjects := False;
    ObjectList1 := TObjectList.Create;
    ObjectList2 := TObjectList.Create;
  end;

  function TDoubleObjList.Data1GetData2(AObject: TObject): TObject;
  begin
    Result := ObjectList2.Items[IndexOfData1(AObject)];
  end;

  function TDoubleObjList.Data2GetData1(AObject: TObject): TObject;
  begin
    Result := ObjectList1.Items[IndexOfData2(AObject)];
  end;

  destructor TDoubleObjList.Destroy;
  begin
    ObjectList1.Free;
    ObjectList2.Free;
    inherited;
  end;

  function TDoubleObjList.IndexOfData1(AObject: TObject): Integer;
  begin
    Result := ObjectList1.IndexOf(AObject);
  end;

  function TDoubleObjList.IndexOfData1(AIndex: Integer): TObject;
  begin
    Result := ObjectList1.Items[AIndex];
  end;

  function TDoubleObjList.IndexOfData2(AObject: TObject): Integer;
  begin
    Result := ObjectList2.IndexOf(AObject);
  end;

  function TDoubleObjList.IndexOfData2(AIndex: Integer): TObject;
  begin
    Result := ObjectList2.Items[AIndex];
  end;

  procedure TDoubleObjList.SetOwnsObjects(const Value: Boolean);
  begin
    FOwnsObjects := Value;
    ObjectList1.OwnsObjects := Value;
    ObjectList2.OwnsObjects := Value;
  end;

  { TDoubleString }

  procedure TDoubleString.AddString(AString1, AString2: string);
  begin
    Strings1.Add(AString1);
    Strings2.Add(AString2);
  end;

  procedure TDoubleString.Clear;
  begin
    Strings1.Clear;
    Strings2.Clear;
  end;

  function TDoubleString.Count: Integer;
  begin
    Result := Strings1.Count;
  end;

  constructor TDoubleString.Create;
  begin
    Strings1 := TStringList.Create;
    Strings2 := TStringList.Create;
  end;

  function TDoubleString.Data1GetData2(AString: string): string;
  begin
    Result := Strings2.Strings[IndexOfData1(AString)];
  end;

  function TDoubleString.Data2GetData1(AString: string): string;
  begin
    Result := Strings1.Strings[IndexOfData2(AString)];
  end;

  destructor TDoubleString.Destroy;
  begin
    Strings1.Free;
    Strings2.Free;
    inherited;
  end;

  function TDoubleString.IndexOfData1(AIndex: Integer): string;
  begin
    Result := Strings1.Strings[AIndex];
  end;

  function TDoubleString.IndexOfData1(AString: string): Integer;
  begin
    Result := Strings1.IndexOf(AString);
  end;

  function TDoubleString.IndexOfData2(AString: string): Integer;
  begin
    Result := Strings2.IndexOf(AString);
  end;

  function TDoubleString.IndexOfData2(AIndex: Integer): string;
  begin
    Result := Strings2.Strings[AIndex];
  end;

  end.

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved