加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

class – TObjectList <>获取项错误

发布时间:2020-12-15 04:08:00 所属栏目:大数据 来源:网络整理
导读:我正在尝试在Delphi XE8中创建TObjectList类,但是当我尝试获取值时,我会收到错误. compiler error message: “[dcc32 Error] : can’t access to private symbol {System.Generics.Collections}TList.GetItem” 这是我的代码: unit Unit2;interfaceuses Cla
我正在尝试在Delphi XE8中创建TObjectList类,但是当我尝试获取值时,我会收到错误.

compiler error message: “[dcc32 Error] : can’t access to private symbol {System.Generics.Collections}TList.GetItem”

这是我的代码:

unit Unit2;
interface
uses
  Classes,System.SysUtils,System.Types,REST.Types,System.JSON,Data.Bind.Components,System.RegularExpressions,System.Variants,Generics.Collections;

  type
  TTruc = class
  public
    libelle : string;
    constructor Create(pLibelle : string);
  end;

  TListeDeTrucs = class(TObjectList<TTruc>)
  private
    function GetItem(Index: Integer): TTruc;
    procedure SetItem(Index: Integer; const Value: TTruc);
  public
    function Add(AObject: TTruc): Integer;
    procedure Insert(Index: Integer; AObject: TTruc);
    procedure Delete(Index: Integer);
    property Items[Index: Integer]: TTruc read GetItem write SetItem; default;
  end;

implementation

{ TTruc }

constructor TTruc.Create(pLibelle: string);
begin
  libelle := pLibelle;
end;

{ TListeDeTrucs }

function TListeDeTrucs.Add(AObject: TTruc): Integer;
begin
  result := inherited Add(AObject);
end;

procedure TListeDeTrucs.Insert(Index: Integer; AObject: TTruc);
begin
  inherited Insert(index,AObject);
end;

procedure TListeDeTrucs.Delete(Index: Integer);
begin
  inherited delete(index);
end;

function TListeDeTrucs.GetItem(Index: Integer): TTruc;
begin
  result := inherited GetItem(index);
end;

procedure TListeDeTrucs.SetItem(Index: Integer; const Value: TTruc);
begin
  inherited setItem(index,value);
end;
end.

测试代码是:

procedure TForm1.Button1Click(Sender: TObject);
var
  l : TListeDeTrucs;
  i : integer;
  Obj : TTruc;
begin
  l := TListeDeTrucs.Create(true);
  l.Add(TTruc.Create('one'));
  l.Add(TTruc.Create('two'));
  Obj := TTruc.Create('three');
  l.Add(Obj);
  for i := 0 to l.count - 1 do
  begin
    showMessage(l[i].libelle);
  end;
  L.Delete(0);
  l.extract(Obj);
  l.Free;
end;

我怎样才能使它工作?

解决方法

好吧,GetItem,实际上SetItem是私有的.您的代码无法看到它们.私人成员只能在宣布他们的单位中看到.您需要使用至少受到保护的成员.

这编译:

function TListeDeTrucs.GetItem(Index: Integer): TTruc;
begin
  Result := inherited Items[Index];
end;

procedure TListeDeTrucs.SetItem(Index: Integer; const Value: TTruc);
begin
  inherited Items[Index] := Value;
end;

在这种情况下,您的类有点无意义,因为您的类中的所有方法都不会改变基类的行为.但是,你真正的班级做得更多.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读