For the latest version of JMP Help, visit JMP.com/help.


Publication date: 04/30/2021

Classes

Classes enable you to create objects based on templates. You can use JSL to create a definition of a class. A class definition is a template, which contains definitions for member variables, methods, and functions. After you have defined a class in JSL, you can instantiate an object that has all of the characteristics defined in the class definition.

Use the JSL function Define Class() to create a JSL class definition. Use the JSL function New Object() to instantiate an instance of a JSL class.

The JSL function Define Class() has a number of arguments that define the contents of the class:

Define Class( "class name",
	<Base Class( "base class name", <"base class name", ...> ),>
	<Show( All( Boolean ) ) | Show( <Members( Boolean ),> <Methods( Boolean ),> <Functions( Boolean )> ),>
	<Assignment Statements>
);

Where:

"class name" is the name of the class being defined.

"base class name" is one or more class names that make the set of base classes that are used as the foundation of the class being created.

Show defines the value that appears when a class object is written out using the Show(), Print(), or Write() functions. Options include:

All( Boolean ) shows all members, methods, and functions defined in a class object instance. The default value for All is true. If you specify the All option, that setting overrides any other arguments in the Show option.

Members( Boolean ) shows all member variables contained in a class object instance.

Methods( Boolean ) shows all methods contained in a class object instance.

Functions( Boolean ) shows all functions contained in a class object instance.

Note: You can specify either the All option or any number of the Members, Methods, and Functions options, but you cannot specify both options at the same time.

Note: If a class contains a _show_ method, that method overrides the Show option.

Assignment Statements are zero or more of the following assignment statements, separated by semicolons:

Member = JSL Expression

Method Name = Method( {<argument, ...>}, body )

Function Name = Function( {<argument, ...}, {<local variable, ...>}, body )

Note: The Assignment Statements can alternatively be specified as a list of JSL expressions.

There are three specialized methods that you can use in a class definition:

_init_

Initializes the object at the time it is instantiated. This is considered the constructor for the class. You can have only one constructor per class.

_show_

Defines the output of the Show(), Print(), and Write() JSL functions when an instance of the class is used as the argument.

_to string_

Defines the output of the Char() JSL function when an instance of the class is used as the argument.

Notes:

All methods, member variables, and functions are public.

Functions defined within a class cannot access method variables or methods within the class.

Example of Defining a Class

The following example defines a simple class for a pet, containing the following information: name, breed, and color.

In this example, you define the constructor to require all information to create a new Pet object. Also, you define a set of accessor methods to obtain each of the attributes of a Pet object.

Define Class( "Pet",
	name = ""; breed = ""; color = "";
	_init_ = Method( { name, breed, color },
		this:name = name;
		this:color = color;
		this:breed = breed;
	);
	_show_ = Method( {},
		Return( name || " is a " || color || " " || breed || "." )
	);
	Get Pet Name = Method( {}, Return( name ) )
);

Instantiating a Class

To instantiate an instance of a JSL class object, use New Object() JSL function with any of the following functions and arguments:

New Object( "Class Name"( constructor arguments ) );
New Object( Class Name( constructor arguments ) );
New Object( Class Reference( constructor arguments ) );
New Object( "Class Name", { constructor arguments } );
New Object( Class Name, { constructor arguments } );
New Object( Class Reference, { constructor arguments } );

Examples of Instantiating and Using a Class

The following example shows how to instantiate a class to create and access JSL class objects.

class = "Pet";
xa = New Object( Pet( "Oliver", "Schnauzer", "Black" ) );
xb = New Object( class, { "Flynn", "Cockapoo", "Brown" } );
xc = New Object( class( "Mandi", "King Charles Cavalier", "Ruby" ) );
xd = New Object( "Pet"( "Charlie", "Asian", "White" ) );
xd1 = New Object( xd( "Fred", "Siamese", "Silver" ) );
xd2 = New Object( xd, { "Sam", "German Shepard", "Black and White" } );
 
Show( xa );
Show( xb );
Show( xc );
Show( xd );
Show( xd1 );
Show( xd2 );
xd2 = xd;                 // returns a second reference to xd
xdclone = xd << Clone;    // copy xd to a new instance, named xdclone
xd2:name = "Jerry";
Show( xd:Get Pet Name(), xd2:Get Pet Name(), xdclone:Get Pet Name() );

This example creates multiple instances of the Pet class. The output of the Show() JSL function is determined by the _show_ method that is defined in the Pet class definition. This example also illustrates the difference between creating a second JSL reference to a class instance and cloning a class instance. When using the Clone message, you are creating a new class instance that is independent of the original. However, the new instance starts out as a copy of the original.

Want more information? Have questions? Get answers in the JMP User Community (community.jmp.com).