var s:TStrings;begin
s:=TStrings.create;try
s.add('xxxxxxxxx');//该行总是出错,提示"抽象错误'finally
s.free;
end;

解决方案 »

  1.   

    TStrings是抽象类,永远不能被实例化的。应该这样:
    s:=TStringList.create;try
    s.add('xxxxxxxxx');//该行总是出错,提示"抽象错误'finally
    s.free;
    end;
      

  2.   

    看看吧: 
                             Tstrings类简单介绍及实例
                      高红岩(ghyghost)在DELPHI的程序开发过程中Tstrings类的使用是比较频繁的,下面就此类在DELPHI5的开发环境中进行一下简单的介绍及实例(注:本文只对tstrings类中的方法及属性进行介绍,从其父类继承的属性及方法不属本文讨论之内)。 
    Add
    原型:function Add(const S: string): Integer; virtual;
    注解:此方法是在字符表中的后面添加字符串,返回值是新添加字符串的索引值。
    实例:
    procedure TForm1.Button1Click(Sender: TObject);
    var
    i:integer;
    begin
    for i:=1 to 10 do
    listbox1.Items.Add(inttostr(i));
    end;
    AddStrings
    原型:procedure AddStrings(Strings: TStrings); virtual;
    注解:从字符表中添加一组字符表。
    实例:
    procedure TForm1.Button2Click(Sender: TObject);
    var
    abc:tstringlist;
    begin
    abc:=tstringlist.Create;
    abc.Assign(listbox1.Items);
    listbox2.Items.AddStrings(abc);
    end;procedure TForm1.FormCreate(Sender: TObject);
    var
    i:integer;
    begin
    for i:=1 to 10 do
    listbox1.Items.Add(inttostr(i));
    end;Append
    原型:procedure Append(const S: string);
    注解:此方法是在字符表中的后面添加字符串。 
    实例:
    procedure TForm1.Button1Click(Sender: TObject);
    begin
    listbox1.Items.Append(’you will win!!’);
    end;Assign
    原型:procedure Assign(Source: TPersistent); override;
    注解:用此方法从另一个兼容的对象中的值赋给此对象,相当于复制和赋值的功能。
    实例:
    procedure TForm1.Button1Click(Sender: TObject);
    var
    i:integer;
    begin
    for i:=1 to 10 do
    begin
    listbox1.items.add(inttostr(i));
    end;
    listbox2.items.Assign(listbox1.items);
    end;BeginUpdate
    原型:procedure BeginUpdate;
    注解:此方法是在字符串列表与可视化列表控件进行操作时使用的,目的是防止添加或删除item时进行刷新(在大数据量进行添加或删除操作时是很费时间的)。与EndUpdate进行配对操作。
    实例:
    procedure TForm1.Button1Click(Sender: TObject);
    var
    i:integer;
    begin
    listbox1.Items.BeginUpdate;
    for i:=0 to 10000 do
    begin
    listbox1.items.Add(inttostr(i));
    end;
    listbox1.Items.EndUpdate;
    end;Clear
    原型:procedure Clear; virtual; abstract;
    注解:此方法清除字符表中全部的内容
    实例:
    procedure TForm1.FormCreate(Sender: TObject);
    var
    i:integer;
    begin
    for i:=0 to 10 do
    listbox1.items.add(inttostr(i));
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
    listbox1.Items.Clear;
    end;Delete
    原型:procedure Delete(Index: Integer); virtual; abstract;
    注解:此方法通过索引值删除指定的字符串。
    实例:
    procedure TForm1.FormCreate(Sender: TObject);
    var
    i:integer;
    begin
    for i:=0 to 10 do
    listbox1.items.add(inttostr(i));
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
    listbox1.Items.Delete(listbox1.ItemIndex);
    end;Destroy
    原型:destructor Destroy; override;
    注解:消毁一个TStrings类的实例。
    实例:
    var
    Form1: TForm1;
    aaa:boolean;
    bbb:tstringlist;
    implementation{$R *.DFM}procedure TForm1.FormCreate(Sender: TObject);
    begin
    aaa:=true;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
    if aaa=true then
    begin
    bbb:=tstringlist.create;
    aaa:=false;
    end
    else
    begin
    bbb.Destroy;
    aaa:=true;
    end;
    end;end.
    EndUpdate
    原型:procedure EndUpdate;
    注解:此方法是在字符串列表与可视化列表控件进行操作时使用的,目的是防止添加或删除item时进行刷新(在大数据量进行添加或删除操作时是很费时间的)。与BeginUpdate进行配对操作。
    实例:
    procedure TForm1.Button1Click(Sender: TObject);
    var
    i:integer;
    begin
    listbox1.Items.BeginUpdate;
    for i:=0 to 10000 do
    begin
    listbox1.items.Add(inttostr(i));
    end;
    listbox1.Items.EndUpdate;
    end;Equals
    原型:function Equals(Strings: TStrings): Boolean;
    注解:此方法为判断两个Tstrings类中的内容是否相当,如果相等返回为true,不等返回为false。
    实例:
    procedure TForm1.FormCreate(Sender: TObject);
    var
    i:integer;
    begin
    for i:=0 to 10 do
    begin
    listbox1.items.add(inttostr(i));
    listbox2.items.add(inttostr(i));
    end;for i:=0 to 9 do
    begin
    listbox3.items.add(inttostr(i));
    end;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
    if listbox1.items.Equals(listbox2.items) then
    showmessage(’相等’)
    else
    showmessage(’不相等’);end;procedure TForm1.Button2Click(Sender: TObject);
    begin
    if listbox2.items.Equals(listbox3.items) then
    showmessage(’相等’)
    else
    showmessage(’不相等’);
    end;Exchange
    原型:procedure Exchange(Index1, Index2: Integer); virtual;
    注解:此方法是借助两个字符串在表中的索引而调换位置。与move区别是(If either string has an associated object, Exchange changes the position of the object as well.如果这个字符串有一个链接的对象,那么这个对象的位置也随着字符串的位置改变而改变。)
    实例:
    procedure TForm1.Button1Click(Sender: TObject);
    var
    i:integer;
    begin
    for i:=0 to 10 do
    listbox1.items.add(inttostr(i));listbox1.items.Exchange(0,10);
    end;GetText
    原型:function GetText: PChar; virtual;
    注解:取得所有此类中的字符。
    实例:
    procedure TForm1.FormCreate(Sender: TObject);
    begin
    listbox1.Items.add(’1’);
    listbox1.Items.add(’1’);
    listbox1.Items.add(’1’);
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
    showmessage(string(listbox1.Items.GetText));
    end;IndexOf
    原型:function IndexOf(const S: string): Integer; virtual;
    注解:通过字符串常量来得到在此类中的索引值。
    实例:
    procedure TForm1.FormCreate(Sender: TObject);
    begin
    listbox1.Items.add(’1’);
    listbox1.Items.add(’2’);
    listbox1.Items.add(’3’);
    listbox1.Items.add(’4’);
    listbox1.Items.add(’5’);
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
    showmessage(inttostr(listbox1.Items.indexof(’5’)));
    end;Insert
    原型:procedure Insert(Index: Integer; const S: string); virtual; abstract;
    注解:在指定的索引位置插入一个字符串。
    实例:
    procedure TForm1.FormCreate(Sender: TObject);
    begin
    listbox1.Items.add(’1’);
    listbox1.Items.add(’2’);
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
    listbox1.Items.Insert(0,’abc’);
    end;LoadFromFile
    原型:procedure LoadFromFile(const FileName: string); virtual;
    注解:从指定的文件名装载文件内容。
    实例:
    procedure TForm1.Button2Click(Sender: TObject);
    begin
    listbox1.items.LoadFromFile(’c:\abc.txt’);
    end;LoadFromStream
    原型:procedure LoadFromStream(Stream: TStream); virtual;
    注解:从流中装载文件。
    实例:
    procedure TForm1.Button1Click(Sender: TObject);
    var
    abc:tmemorystream;
    begin
    abc:=tmemorystream.create;
    abc.LoadFromFile(’c:\csdn.txt’);
    memo1.lines.LoadFromStream(abc);
    end;
    Move
    原型:procedure Move(CurIndex, NewIndex: Integer); virtual;
    注解:交换两个以索引为参数的字符串。与Exchange不同的是(If the string has an associated object, the object remains associated with the string in its new position.如果这个字符串有一个对象,那么这个对象的位置不变,自动链接到新的字符串)
    实例:
    procedure TForm1.Button1Click(Sender: TObject);
    begin
    ListBox1.Items.Move(ListBox1.ItemIndex, 0);
    end;SaveToFile
    原型:procedure SaveToFile(const FileName: string); virtual;
    注解:通过参数来把tstrings中的内容存成文件。
    实例:
    procedure TForm1.Button2Click(Sender: TObject);
    begin
    memo1.Lines.SaveToFile(’c:\abc.txt’);
    end;SaveToStream
    原型:procedure SaveToStream(Stream: TStream); virtual;
    注解:保存成流。
    实例:
    procedure TForm1.Button1Click(Sender: TObject);
    var
    abc:tstringlist;
    abcd:tmemorystream;
    begin
    abc:=tstringlist.create;
    abcd:=tmemorystream.create;
    abc.add(’1’);
    abc.add(’1’);
    abc.add(’1’);
    abc.add(’1’);
    abc.SaveToStream(abcd);
    abcd.Position:=0;
    memo1.lines.loadfromstream(abcd);
    end;
    SetText
    原型:procedure SetText(Text: PChar); virtual;
    注解:设置tstrings类中的内容。
    实例:
    procedure TForm1.FormCreate(Sender: TObject);
    var
    i:integer;
    begin
    for i:=0 to 10 do
    begin
    listbox1.items.add(inttostr(i));
    end;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
    listbox1.Items.SetText(pchar(’aa’));
    end;
      

  3.   

    var s:TStringList;
    begin
      s:=TStringList.Create;
      s.Add('test');
      ...
      

  4.   

    To li_zhifu(东北人):
    s可以是TStrings类型,而且用TStrings类型比TStringList类型更好。
      

  5.   

    1.TStringList从何而来?
    2.TStrings.text何用处?
      

  6.   

    1、TStingList是TStrings的派生类,TStrings是一个抽象类,他只提供类的框架,但没有实现
    2、TStrings.text从派生类来
      

  7.   

    帮助中说明得很清楚:
    TStrings is an abstract base class for objects that represent a list of strings.
    为一个抽象类,不能实例化。
    只能从其继承的子类中进行实例。
    好处是:
    针对不同的字符串操作用不同的类。
      

  8.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
    aa:tstrings;
    begin
    aa:=tstringlist.Create;
    aa.text:='asdf';
    showmessage(aa[0]);
    end;可以这样写
      

  9.   

    如何获得一个树控件下面的未展开的所有子项?
    (比如DELPHI提供的许多目录控件,现想获得网络邻居上所有计算机名称)
      

  10.   

    来晚了.
    TStrings是抽象类,抽象类是不能实例化的.可用其派生类TStringList.