Delphi 为什么不显示输出?
我今天开始学习课程和对象编程。手册中有代码,我必须复制才能运行和保存。我需要创建一个类(TLine)并使用该类实例化一个对象 问题:my RichEdit组件中未显示任何输出。我将代码完全从书中复制到delphi,但没有显示输出 输出的外观:“***********” 我的班级:Delphi 为什么不显示输出?,delphi,Delphi,我今天开始学习课程和对象编程。手册中有代码,我必须复制才能运行和保存。我需要创建一个类(TLine)并使用该类实例化一个对象 问题:my RichEdit组件中未显示任何输出。我将代码完全从书中复制到delphi,但没有显示输出 输出的外观:“***********” 我的班级: unit Lines_U; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dia
unit Lines_U;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
Type
TLine = Class
Public
fSize : integer;
fPattern : char;
public
Constructor Create;
Procedure Draw(Var line: string);
end;
implementation
{ TLine }
Constructor TLine.Create;
begin
fSize := 10;
fPattern := '*';
end;
Procedure TLine.Draw(Var line: string);
Var
loop : integer;
begin
for loop := 1 to fSize do
begin
line := line + fPattern;
end;
end;
end.
用于实例化TLine类的对象的代码:
unit UseLine_U;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Lines_U, StdCtrls, ComCtrls;
type
TForm1 = class(TForm)
redOut: TRichEdit;
Procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
line : TLine;
implementation
{$R *.dfm}
Procedure TForm1.FormCreate(Sender: TObject);
Var tempLine : string;
begin
line := TLine.Create;
line.Draw(tempLine);
redOut.Lines.Add(tempLine);
end;
end.
代码未运行的原因是事件处理程序
Form1.FormCreate
未链接到OnCreate
事件。恢复对象检查器中的链接
关于事件处理程序永远不要手动编写事件处理程序(所有这些过程都以…上的
开始)。始终使用对象检查器创建它们
如果双击某个事件,Delphi将为您创建一个代码模板,您可以在其中填入数据。
确保在对象检查器中填写事件处理程序。否则,它们将不起作用(如您所见)。
如果要删除事件处理程序,请不要在对象检查器中删除它,而是将事件处理过程中的代码还原为空模板。
Delphi将看到它是空的,并在下一次编译时将其删除
关于您的代码
除了丢失的链接之外,您的代码没有任何问题。它运行得很好。
尽管有一些风格问题,这些问题与运营无关,但仍然很重要
下面是我将如何重写您的代码
unit Lines_U;
interface
//only import units that you actually use.
type //please type reserved words in all lowercase, this is Pascal not VB.
TLine = class
private //make data members private.
fSize : integer;
fPattern : char;
public
constructor Create;
procedure Draw(var line: string);
property Size: integer read fSize write fSize; //Use properties to expose data members.
property Pattern: char read fPattern write fPattern;
end;
implementation
{ TLine }
constructor TLine.Create;
begin
inherited; //make the inherited call in your constructor explicit.
fSize := 10;
fPattern := '*';
end;
procedure TLine.Draw(var line: string);
//var
//loop : integer; //use consistent indentation
begin
//Changing a string ten times in a row is inefficient.
//try to do your changes all at once.
//for loop := 1 to fSize do begin
// line := line + fPattern;
//end;
Line:= Line + StringOfChar(fPattern, fSize);
end;
end.
您的表格:
unit UseLine_U;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, Lines_U;
//put your own unit last, to prevent name clashes with built in classes and functions.
type
TForm1 = class(TForm)
//note that the {nothing} line is really **published**.
//And data members should be private
//Line : TLine; //Line should be private.
RedOut: TRichEdit;
procedure FormCreate(Sender: TObject);
private
//Prefix all private data with `F` for Field.
FLine: TLine; //Line should be a item in the form, not a global var.
public
property Line: TLine read FLine; //read only access to line.
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var
tempLine : string;
i: integer;
begin
//tempLine:= ''; //local variables should be initialized.
//However strings are always initialized to '', because they are managed types.
//everything else will contain random data unless you fill it!
FLine := TLine.Create;
Line.Draw(tempLine);
i:= 0; //init i, otherwise it will be random!
while i < 5 do begin //always use `begin-end` in loops, never a naked `do`
RedOut.Lines.Add(tempLine);
i:= i + 1;
end; {while} //I like to annotate my loop `end`s, but that's just me.
FreeAndNil(FLine); //Dispose of TLine when you're done with it.
end;
end.
unituseline\U;
接口
使用
窗口、消息、系统工具、变体、类、图形、控件、窗体、,
对话框、STDTRL、通讯器、行;
//将您自己的单元放在最后,以防止与内置类和函数发生名称冲突。
类型
TForm1=类(TForm)
//注意,{nothing}行实际上是**已发布**。
//数据成员应该是私有的
//行:TLine//线路应该是私人的。
重做:权宜之计;
过程表单创建(发送方:ToObject);
私有的
//在所有私有数据前面加上“F”作为字段前缀。
弗林:特琳//行应该是表单中的项,而不是全局变量。
公众的
建筑红线:TLine read FLine//对行的只读访问。
结束;
变量
表1:TForm1;
实施
{$R*.dfm}
过程TForm1.FormCreate(发送方:TObject);
变量
模板:字符串;
i:整数;
开始
//模板:=''//应该初始化局部变量。
//但是,字符串始终初始化为“”,因为它们是托管类型。
//除非您填写,否则其他所有内容都将包含随机数据!
FLine:=TLine.Create;
线条绘制(模板线);
i:=0//init i,否则它将是随机的!
当我<5 do begin//时,总是在循环中使用'begin end',而不是裸的'do'`
RedOut.line.Add(模板行);
i:=i+1;
结束;{while}//我喜欢注释我的循环'end',但那只是我自己。
FreeAndNil(FLine)//处理完毕后,请将TLine处理掉。
结束;
结束。
我可以考虑其他事情,但我不想让您负担过重。您的代码没有运行的原因是您的事件处理程序Form1。FormCreate
没有链接到OnCreate
事件。恢复对象检查器中的链接
关于事件处理程序
永远不要手动编写事件处理程序(所有这些过程都以…
上的开始)。始终使用对象检查器创建它们
如果双击某个事件,Delphi将为您创建一个代码模板,您可以在其中填入数据。
确保在对象检查器中填写事件处理程序。否则,它们将不起作用(如您所见)。
如果要删除事件处理程序,请不要在对象检查器中删除它,而是将事件处理过程中的代码还原为空模板。
Delphi将看到它是空的,并在下一次编译时将其删除
关于您的代码
除了丢失的链接之外,您的代码没有任何问题。它运行得很好。
尽管有一些风格问题,这些问题与运营无关,但仍然很重要
下面是我将如何重写您的代码
unit Lines_U;
interface
//only import units that you actually use.
type //please type reserved words in all lowercase, this is Pascal not VB.
TLine = class
private //make data members private.
fSize : integer;
fPattern : char;
public
constructor Create;
procedure Draw(var line: string);
property Size: integer read fSize write fSize; //Use properties to expose data members.
property Pattern: char read fPattern write fPattern;
end;
implementation
{ TLine }
constructor TLine.Create;
begin
inherited; //make the inherited call in your constructor explicit.
fSize := 10;
fPattern := '*';
end;
procedure TLine.Draw(var line: string);
//var
//loop : integer; //use consistent indentation
begin
//Changing a string ten times in a row is inefficient.
//try to do your changes all at once.
//for loop := 1 to fSize do begin
// line := line + fPattern;
//end;
Line:= Line + StringOfChar(fPattern, fSize);
end;
end.
您的表格:
unit UseLine_U;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, Lines_U;
//put your own unit last, to prevent name clashes with built in classes and functions.
type
TForm1 = class(TForm)
//note that the {nothing} line is really **published**.
//And data members should be private
//Line : TLine; //Line should be private.
RedOut: TRichEdit;
procedure FormCreate(Sender: TObject);
private
//Prefix all private data with `F` for Field.
FLine: TLine; //Line should be a item in the form, not a global var.
public
property Line: TLine read FLine; //read only access to line.
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var
tempLine : string;
i: integer;
begin
//tempLine:= ''; //local variables should be initialized.
//However strings are always initialized to '', because they are managed types.
//everything else will contain random data unless you fill it!
FLine := TLine.Create;
Line.Draw(tempLine);
i:= 0; //init i, otherwise it will be random!
while i < 5 do begin //always use `begin-end` in loops, never a naked `do`
RedOut.Lines.Add(tempLine);
i:= i + 1;
end; {while} //I like to annotate my loop `end`s, but that's just me.
FreeAndNil(FLine); //Dispose of TLine when you're done with it.
end;
end.
unituseline\U;
接口
使用
窗口、消息、系统工具、变体、类、图形、控件、窗体、,
对话框、STDTRL、通讯器、行;
//将您自己的单元放在最后,以防止与内置类和函数发生名称冲突。
类型
TForm1=类(TForm)
//注意,{nothing}行实际上是**已发布**。
//数据成员应该是私有的
//行:TLine//线路应该是私人的。
重做:权宜之计;
过程表单创建(发送方:ToObject);
私有的
//在所有私有数据前面加上“F”作为字段前缀。
弗林:特琳//行应该是表单中的项,而不是全局变量。
公众的
建筑红线:TLine read FLine//对行的只读访问。
结束;
变量
表1:TForm1;
实施
{$R*.dfm}
过程TForm1.FormCreate(发送方:TObject);
变量
模板:字符串;
i:整数;
开始
//模板:=''//应该初始化局部变量。
//但是,字符串始终初始化为“”,因为它们是托管类型。
//除非您填写,否则其他所有内容都将包含随机数据!
FLine:=TLine.Create;
线条绘制(模板线);
i:=0//init i,否则它将是随机的!
当我<5 do begin//时,总是在循环中使用'begin end',而不是裸的'do'`
RedOut.line.Add(模板行);
i:=i+1;
结束;{while}//我喜欢注释我的循环'end',但那只是我自己。
FreeAndNil(FLine)//处理完毕后,请将TLine处理掉。
结束;
结束。
我可以想其他事情,但我不想让你负担过重。这个问题在这个网站上是离题的,贝卡