Chapter 5 ObjectScript

Chapter 5 ObjectScript

Both methods and routines can be ObjectScriptwritten using , but most modern code is written using methods. Methods are contained in classes, which allows similar methods to be grouped together, automatic generation of documentation in the class reference, and the use IRISof object-oriented features of .

This doesn't mean routinesit's not important. Many useful system utilities are written as routines, and routines are generated when a class is compiled.

Example class

Shown below is a User.DemoClasssample class named containing ObjectScriptmethods written in . This example gives us a chance to look at some common ObjectScriptcommands, operators, and functions, and see how code is organized within methods.

Class User.DemoClass
{
    
    

/// Generate a random number.
/// This method can be called from outside the class.
ClassMethod Random() [ Language = objectscript ]
{
    
    
    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
}

/// Input a number.
/// This method can be called from outside the class.
ClassMethod Input() [ Language = objectscript ]
{
    
    
    read "Enter a number from 1 to 10: ", input
    set name=..GetNumberName(input)
    write !, "Name of this number: "_name
}

/// Given an number, return the name.
/// This method can be called only from within this class.
ClassMethod GetNumberName(number As %Integer) As %Integer [ Language = objectscript, Private ]
{
    
    
    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 method can be called from outside the class.
ClassMethod Interesting() [ Language = objectscript ]
{
    
    
    write "Today's date: "_$ZDATE($HOROLOG,3)
    write !,"Your installed version: "_$ZVERSION
    write !,"Your username: "_$USERNAME
    write !,"Your security roles: "_$ROLES
}

}

Please note the following points:

  • Random()and Input()the method call GetNumberName()method, which is a private method of this class and cannot be called from outside the class.
  • WRITE, QUIT, SETand READare commands. The language also includes other commands for deleting variables, for controlling program flow, for controlling I/Odevices, for managing transactions (possibly nested), and so on.

Command names are not case-sensitive, although by convention they appear in all uppercase in run text.

  • This example includes two operators. The plus sign ( +) performs addition, and the underscore ( _) performs string concatenation.

ObjectScriptProvides commonly used operators and some special operators not seen in other languages.

  • $RANDOM, $CASEand $ZDATEare functions.

The language provides functions for string operations, various conversions, formatting operations, mathematical operations, etc.

  • $HOROLOG, $ZVERSION, $USERNAMEand $ROLESare system variables ( IRIScalled special variables in ). Most special variables contain IRISvalues ​​for the operating environment, current processing status, etc.
  • ObjectScriptComment lines, block comments, and comments at the end of statements are supported.

We can execute the methods of this class in the terminal as a demonstration. In these examples, <TESTNAMESPACE>it's the prompt displayed in the terminal. The text following the prompt on the same line is the command entered. The lines after that show the values ​​that the system writes to the terminal in response.

TESTNAMESPACE>do ##class(User.DemoClass).Input()
Enter a number from 1 to 10: 7
Name of this number: seven
TESTNAMESPACE>do ##class(User.DemoClass).Interesting()
Today's date: 2021-07-15
Your installed version: IRIS for Windows (x86-64) 2019.3 (Build 310U) Mon Oct 21 2019 13:48:58 EDT
Your username: SuperUser
Your security roles: %All
TESTNAMESPACE>

Guess you like

Origin blog.csdn.net/yaoxin521123/article/details/132419926