(类的问题)大侠们,能不能给我讲讲下面这个简单的程序为什么有错? Tstrings是一个虚类不能生成实例的,你要用它的子类(派生类)所以,呵呵用TStringlist就像Memo的Item属性就是TStringlist类型的 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 TStrings的源码申明去看一看嘛,很多方法申明成Virtual; Abstract;的。这些抽象方法必须在子类中重载的。用TStringList就可以了。unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Button1: TButton; Edit1: TEdit; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } MyString : TStringList; end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);begin MyString:=TStringList.Create; MyString.Insert(0,'Hi!'); Edit1.Text:=IntToStr(MyString.Count); MyString.Free;end;end. 我想问一下,我模拟tcustommemo的lines的实现方法自定义了一个类,在调用时仍然报错,不知怎么回事,代码如下:unit Unit1;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type Trick = class(Tcustomedit) private fline:Tstrings; protected procedure setl(v:Tstrings); public property line:Tstrings read fline write setl; end; TForm1 = class(TForm) Button1: TButton; Edit1: TEdit; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } a:Trick; end;var Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);begina.line.Add('hello');end;{ Trick }procedure Trick.setl(v: Tstrings);beginfline.Assign(v);end;end. fline没有初始化,你应该在FormCreate中执行:fline := TStringList.Create;在FormDestroy中执行:fline.Free; 同意aningstar(乘风破浪终有时) 。 谢谢大家,我明白了。以前看书,发现TStrings和TStringList都是混着讲的,一会儿这个一会儿那个,总是不明白。其实很简单吗,TStrings中很多Properties和Method都是Virtual的呀。 女孩啊,早知道多上心!^_^不只是virtual, 主要还是abstract的。 如何取得Excel中活动的工作表个数 sqlserver 数据库 显示平均值的问题 超级难题:如何取出下级所有部门代码 这个存储过程怎么改进?急! 如何设置界面上的一个,点让这个点看不到! 咳!超级新手来提问!字符串问题 表格移动同步问题 最小化是触发哪个事件? 请问如何实现:将一个自定义控件在设计期放在窗体上,它能使其他的控件不能移动? delphi dbgrid的双击问题 delphi里有没有c中的静态变量? 又有一问题想请教.
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
MyString : TStringList;
end;var
Form1: TForm1;implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
MyString:=TStringList.Create;
MyString.Insert(0,'Hi!');
Edit1.Text:=IntToStr(MyString.Count);
MyString.Free;
end;
end.
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;type
Trick = class(Tcustomedit)
private
fline:Tstrings;
protected
procedure setl(v:Tstrings);
public
property line:Tstrings read fline write setl;
end; TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
a:Trick;
end;
var
Form1: TForm1;
implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);
begin
a.line.Add('hello');
end;{ Trick }procedure Trick.setl(v: Tstrings);
begin
fline.Assign(v);
end;end.
fline.Free;
以前看书,发现TStrings和TStringList都是混着讲的,一会儿这个一会儿那个,总是不明白。其实很简单吗,TStrings中很多Properties和Method都是Virtual的呀。