Purpose of this chapter.
We will learn about the setter which is the basic elements that make up the class definition.
The setter is the interface to call the class member. It use the format of the assignment to a variable. The main purpose is to provide the ability to access field.
Assignment to the setter will be automatically converted to the call of the method of a single argument that does not have a return value.
First, let us look at the setter definition part of Circle.88.
// -----------------------------------------------Getters and setters.
open getter bool Fitted
return _fitted.Clone(); // Encapsulation.
open getter bool Moving
// x-coordinate of the center.
open getter int X
return _x.Clone(); // Encapsulation.
open setter X(int value)
_x.Value = value; // Encapsulation.
// y-coordinate of the center.
open getter int Y
return _y.Clone(); // Encapsulation.
open setter Y(int value)
_y.Value = value; // Encapsulation.
_currentCircle.X = x; // 77th line at the Sample.cook
The assignment to the setter name 'X' will be converted to the call of the 'open setter X(int value)'.
A definition of the setter is the following format.
access-control(open or closed) setter setter-name ( argument )
Statements of the setter definition...
The setter definition is the definition that starts with access control and the 'setter' keyword. And, it ends with 'endsetter' keyword.
The access level of the setter will be specified using the 'open' or 'closed' keyword.
If the 'open' keyword is specified, the setter can be used anywhere in the program.
If the 'closed' keyword is specified, the setter is available only the inside of the class definition.
The 'closed' members of all instances of the same class can be accessed at the inside the class definition.
The 'setter' keyword indicates that this definition is a setter definition. It must be specified at the next of the access-control.
The setter-name must specify at the next of the 'setter' keyword. The naming rule of the setter name is the same as the identifier.(Ver 188.8.131.52 changed) slightly different from identifiers.
Usually, keywords, Rice base class names, and embedded class names cannot be identifiers. However, you can use the name of embedded classes for the setter name.
This change is to prevent naming conflict between newly included classes and members of existing user-defined classes .
A setter of the same name that has different argument can be defined.
open setter X(long value)
_x.Value = value.I; // Cast and encapsulation.
Setter name 'X' already exists. However, the program will operate normally even if the new setter be added to Circle.88.
The argument of the setter must be specified at the next of the setter-name, it must be enclosed by parentheses.
The argument is specified by the set of class-name (type of the argument) and argument-name. An argument is required for the setter. Type of argument will be the type of value that can be assigned to the setter.
Rice will distinguish the setter that has the same name by the type of argument. The setter that has the same name and the different argument can be defined any number.
The Rice creates a name for calling a setter from the setter name and the argument.
open setter X(int value) -> "X(int)"
open setter X(long value) -> "X(long)"
Please note that the access control does not related to the generation of the name.
The 'endsetter' keyword indicates the end of the setter definition.