Controlling access for instance variables
Instance variables have access settings that provide control
over how other objects’ scripts access them.
You can specify that a variable is:
- Public Accessible to any other object
- Protected Accessible only in scripts for the object and its descendants
- Private Accessible in scripts for the object only
For example:
1 |
public integer ii_currentvalue |
1 |
CONSTANT public integer WARPFACTOR = 1.2 |
1 |
protected string is_starship |
1 |
1 |
// Private values used in internal calculations |
1 |
private integer ii_maxrpm |
1 |
private integer ii_minrpm |
You can further qualify access to public and protected variables
with the modifiers: PRIVATEREAD, PRIVATEWRITE, PROTECTEDREAD, PROTECTEDWRITE:
1 |
public privatewrite ii_averagerpm |
Private variables for encapsulation
One use of access settings is to keep other scripts from changing
a variable when they should not. You can use PRIVATE or PUBLIC PRIVATEWRITE
to keep the variable from being changed directly. You might write
public functions to provide validation before changing the variable.
Private variables allow you to encapsulate an object’s
functionality. This technique means that an object’s data
and code are part of the object itself and the object determines
the interface it presents to other objects.
If you generate a component, such as a Jaguar or COM component,
from a custom class user object, you can choose to expose its instance
variables in the component’s interface, but private and
protected instance variables are never exposed.
For more information
For more about access settings, see the chapter about declarations
in the PowerScript Reference
.
For more about encapsulation, see Chapter 2, “Selected Object-Oriented Programming
Topics”.