
内容源自Delphi XE5 UPDATE 2官方帮助《Delphi Reference》,本人水平有限,欢迎大家高人修正相关错误!
也欢迎大家加入到Delphi学习资料汉化中来,有兴趣者可QQ:34484690@qq.com
2 Example Programs
2实例程序
The examples that follow illustrate basic features of Delphi programming. The examples show simple applications that would not normally be compiled from the IDE; you can compile them from the command line.
下面的例子演示 Delphi编程的基本特征,该实例显示简洁的应用程序,通常不会从IDE中编译,你可以在命令行编译他们。
2.1 A Simple Console Application
2.1一个简单的控制台程序
The program below is a simple console application that you can compile and run from the command prompt:
下面是一个简单的控制台程序,你可以从命令行编译并运行它。
program Greeting; {$APPTYPE CONSOLE} var MyMessage: string; begin MyMessage := 'Hello world!'; Writeln(MyMessage); end.
The first line declares a program called Greeting. The {$APPTYPE CONSOLE} directive tells the compiler that this is a console application, to be run from the command line. The next line declares a variable called MyMessage, which holds a string. (Delphi has genuine string data types.) The program then assigns the string "Hello world!" to the variable MyMessage, and sends the contents of MyMessage to the standard output using the Writeln procedure. (Writeln is defined implicitly in the System unit, which the compiler automatically includes in every application.)
第一行声明程序叫做Greeting。{$APPTYPE CONSOLE} 指示字告诉编译器,这是一个控制台程序,它要从命令行运行。接下来的一行声明了一个变量MyMessage,它传输一个字符串(Delphi包含真正的字符串类型)。程序把字符串”Hello world!” 赋给变量MyMessage,然后使用Writeln 例程把MyMessage 的内容送到标准输出设备(Writeln 在System 单元声明,编译器在每个程序中手动包含这个单元)。
You can type this program into a file called greeting.pas or greeting.dpr and compile it by entering:

你可以把这个程序(源代码)输入到一个叫Greeting.pas 或Greeting.dpr 的文件中,然后在控制台输入如下命令编译它:
dcc32 greeting
to produce a Win32 executable.
在Win32平台上执行。
The resulting executable prints the message Hello world!
执行的结果是输出信息 Hello world!
Aside from its simplicity, this example differs in several important ways from programs that you are likely to write with Embarcadero development tools.
除了简单,上面这个事例和我们在 Embarcadero开发软件下写的程序有几个重要的不同。
First, it is a console application. Embarcadero development tools are most often used to write applications with graphical interfaces; hence, you would not ordinarily call Writeln. Moreover, the entire example program (save for Writeln) is in a single file. In a typical GUI application, the program heading the first line of the example would be placed in a separate project file that would not contain any of the actual application logic, other than a few calls to routines defined in unit files.
首先,这是一个控制台程序delphi xe7教程,Embarcadero开发软件通常建立图形界面的程序,因此delphi xe7教程,你不大可能调用Writeln(GUI 程序不能调用Writeln);而且,整个程序只有一个文件。在一个典型的程序中,程序头,也就是例子中的第一行,将被放到一个单独的工程文件中。工程文件一般不包括实际的程序逻辑,而并非调用在单元文件中定义的方式。
2.2 A More Complicated Example
2.2一个稍微复杂的程序
The next example shows a program that is divided into two files: a project file and a unit file. The project file, which you can save as greeting.dpr, looks like this:
下面的例子将程序被分成两个文件:一个工程文件,一个单元文件。工程文件可以存为Greeting.dpr,它看起来这样:

program Greeting; {$APPTYPE CONSOLE} uses Unit1; begin PrintMessage('Hello World!'); end.
The first line declares a program called greeting, which, once again, is a console application. The uses Unit1; clause tells the compiler that the program greeting depends on a unit called Unit1. Finally, the program calls the PrintMessage procedure, passing to it the string Hello World! The PrintMessage procedure is defined in Unit1. Here is the source code for Unit1, which must be saved in a file called Unit1.pas:
第一行声明程序叫做Greeting,它相同是个控制台程序。uses Unit1; 子句告诉编译器,Greeting包含(引用)一个叫做Unit1 的单元。最后,程序调用PrintMessage 过程,并把字符串”Hello world!”传递给它。请注意,PrintMessage 过程是从那里来的?它是在Unit1 单元定义的。下面是Unit1 单元的源代码,你能把它保存在一个叫Unit1.pas 的文件中:
unit Unit1; interface procedure PrintMessage(msg: string); implementation procedure PrintMessage(msg: string); begin Writeln(msg); end; end.
Unit1 defines a procedure called PrintMessage that takes a single string as an argument and sends the string to the standard output. (In Delphi, routines that do not return a value are called procedures. Routines that return a value are called functions.)
Unit1 单元定义一个叫做PrintMessage 的过程,它接收一个字符串作为参数,并把它送到标准输出设备(在Delphi中,没有返回值的示例叫过程,有返回值的类库叫变量)。
Notice that PrintMessage is declared twice in Unit1. The first declaration, under the reserved word interface, makes PrintMessage available to other modules (such as greeting) that use Unit1. The second declaration, under the reserved word implementation, actually defines PrintMessage.
请注意,PrintMessage在Unit1 中声明了两次,第一次是在关键字interface 的上面,这促使它针对引用Unit1 单元的其他模块(比如Greeting)是可用的;第二次声明是在关键字implementation 的上面,它实际定义PrintMessage 过程。
You can now compile Greeting from the command line by entering
现在你可以在控制台输入如下命令编译 Greeting。
dcc32 greeting
to produce a Win32 executable.
在Win32平台上执行。
There is no need to include Unit1 as a command-line argument. When the compiler processes greeting.dpr, it automatically looks for unit files that the greeting program depends on. The resulting executable does the same thing as our first example: it prints the message Hello world!
本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/jisuanjixue/article-123046-1.html
还没朋友
第一我们应该在南沙更多的布置军事防御力量
脱肛真的不要成本
原料环节不能有蛆吗
伊拉克跟俄合作