第六章 ObjectScript - Routine示例
Routine示例
下面显示了一个名为 demoroutine
的示例例程,它是用 ObjectScript
编写的。它包含的过程与上一节示例类中所示的方法执行完全相同的操作。
; this is demoroutine
write "Use one of the following entry points:"
write !,"random"
write !,"input"
write !,"interesting"
quit
//this procedure can be called from outside the routine
random() public {
set rand=$RANDOM(10)+1 ; rand is an integer in the range 1-10
write "Your random number: "_rand
set name=$$getnumbername(rand)
write !, "Name of this number: "_name
}
//this procedure can be called from outside the routine
input() public {
read "Enter a number from 1 to 10: ", input
set name=$$getnumbername(input)
write !, "Name of this number: "_name
}
//this procedure can be called only from within this routine
getnumbername(number) {
set name=$CASE(number,1:"one",2:"two",3:"three",
4:"four",5:"five",6:"six",7:"seven",8:"eight",
9:"nine",10:"ten",:"other")
quit name
}
/* write some interesting values
this procedure can be called from outside the routine
*/
interesting() public {
write "Today's date: "_$ZDATE($HOROLOG,3)
write !,"Your installed version: "_$ZVERSION
write !,"Your username: "_$USERNAME
write !,"Your security roles: "_$ROLES
}
请注意以下要点:
- 唯一真正以插入符号 (
^
) 开头的标识符是globals
的名称。但是,在运行文本和代码注释中,习惯上引用例程就好像其名称以插入符号开头一样,因为在调用例程时使用插入符号(如本页后面所示)。例如,例程demoroutine
通常称为^demoroutine
。 - 例程名称不必包含在例程中。然而,许多程序员将例程名称作为注释包含在例程的开头或作为例程中的第一个标签。
- 该例程有多个标签:
random
、input
、getnumbername
和interesting
。
标签用于指示过程(如本示例中所示)、函数和子例程的起点。还可以将它们用作某些命令的目标。
标签在例程中很常见,但也可以在方法中使用它们。
标签也称为入口点或标签。
-
random
和input
子例程调用getnumbername
子例程,该子例程是例程私有的。
我们可以在终端中执行此例程的部分内容,作为演示。首先,下面显示了一个终端会话,我们在其中运行例程本身。
TESTNAMESPACE>do ^demoroutine
Use one of the following entry points:
random
input
TESTNAMESPACE>
当我们运行例程时,我们只是获得帮助信息,如所见。不需要以这种方式编写例程,但这是常见的。请注意,例程在第一个标签之前包含 QUIT
,以确保当用户调用例程时,处理在该标签之前停止。这种做法也不是必需的,但也很常见。
接下来,下面显示了几个子例程的行为方式:
TESTNAMESPACE>do input^demoroutine
Enter a number from 1 to 10: 7
Name of this number: seven
TESTNAMESPACE>do interesting^demoroutine
Today's date: 2018-02-06
Your installed version: IRIS for Windows (x86-64) 2018.1 (Build 513U) Fri Jan 26 2018 18:35:11 EST
Your username: _SYSTEM
Your security roles: %All
TESTNAMESPACE>
方法可以包含与例程相同的语句、标签和注释。也就是说,这里有关例程内容的所有信息也适用于方法的内容。
网友评论