Originale-mail to me for new edition

 

A simple console application

 

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. (Object Pascal 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.)

You can type this program into a file called Greeting.pas or Greeting.dpr and compile it by entering

 

On Delphi: DCC32 Greeting

On Kylix: dcc Greeting

 

on the command line. The resulting executable prints the message “Hello world!”

Aside from its simplicity, this example differs in several important ways from programs that you are likely to write with Borland development tools. First, it is a console application. Borland development tools are typically 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 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 methods defined in unit files.

 

Topic groups

Using Object Pascal

Program organization: Overview

Pascal source files

Other files used to build applications

Compiler-generated files

About example programs

A simple console application

A more complicated example

A native application

 

译文

 

简单的控制台应用程序

 

下面的程序是一个简单的控制台应用程序,你可以通过命令提示符的方式编译并运行该程序:

 

program Greeting;

 

{$APPTYPE CONSOLE}

 

var MyMessage: string;

 

begin

  MyMessage := 'Hello world!';

  Writeln(MyMessage);

end.

 

程序的第一行声明了一个名为Greeting的程序。{$APPTYPE CONSOLE}是一个编译指示,该指示告诉编译器当前程序是一个控制台应用程序,将通过命令行的方式运行。接着声明了一个名为MyMessage的变量,该变量用于存储字符串。(Object Pascal有真正的字符串数据类型。)然后程序首先将字符串“Hello world!”赋值到变量MyMessage,并使用Writeln过程将MyMessage的内容发送到标准输出。(Writeln过程的定义隐含在System单元中,编译器自动将该单元包含到每个应用程序中。)

你可以键入该程序到一个名为Greeting.pasGreeting.dpr的文件中,并在命令行中输入如下命令对该程序进行编译

 

对于Delphi,输入:    DCC32 Greeting

对于Kylix,输入:     dcc Greeting

 

编译结果是一个可执行文件,该可执行文件在运行时将显示信息“Hello world!”。

与用Borland开发工具编写的程序相比,该范例除简单而外, 还具有一些重要差异。首先,这是一个控制台应用程序。通过图形界面编写应用程序是Borland开发工具的典型用途;因此不能以常规的方式调用Writeln过程。此外,整个程序(保存为Writeln)是一个单独的文件。在典型的应用程序中,该范例中作为程序首部的第一行代码将被单独置于一个工程文件中不包括任何的应用程序逻辑,而只是调用单元文件中极少的几个方法(method)。

 

主题组

使用Object Pascal

程序组织:概述

Pascal源文件

用于建立应用程序的其他文件

编译器产生的文件

关于范例程序

简单的控制台应用程序

相对复杂的范例

本地应用程序范例

 

 

编者注

尽管可以使用任何支持文本编辑的编辑器来编写Object Pascal程序的源代码,但毕竟使用Borland集成开发环境要方便得多。例如,Borland集成开发环境中的代码编辑器可以用不同的字体(含字体颜色)对保留字、字符串、数字等等加以区分,从而是源代码更加清晰易读。

从上面的范例中可以看到传统的Turbo Pascal编程的痕迹,如变量声明、程序语句等全部在一个文件中。试想,如果完成较多的功能,那么一个单独的文件将会有成百上千行源代码,对于编写和维护来说都是相当困难的。由此可见,虽然Object Pascal语言允许开发者自由安排程序组织,但毕竟科学合理的程序组织在编写、维护等效率方面具有明显优势。