第六章 ObjectScript - Routine示例

2023-08-22  本文已影响0人  Cache技术分享

第六章 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  
    }

请注意以下要点:

标签用于指示过程(如本示例中所示)、函数和子例程的起点。还可以将它们用作某些命令的目标。

标签在例程中很常见,但也可以在方法中使用它们。

标签也称为入口点或标签。

我们可以在终端中执行此例程的部分内容,作为演示。首先,下面显示了一个终端会话,我们在其中运行例程本身。

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>

方法可以包含与例程相同的语句、标签和注释。也就是说,这里有关例程内容的所有信息也适用于方法的内容。

上一篇 下一篇

猜你喜欢

热点阅读