Home About Rice Downloads Documents Others Cooker CookerGX
Documents  >  Tutorial  >  setters
Setter

Purpose of this chapter.

We will learn about the setter which is the basic elements that make up the class definition.

Setter

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.

1:

// -----------------------------------------------Getters and setters.

2:

open getter bool Fitted

3:

return _fitted.Clone(); // Encapsulation.

4:

endgetter

5:

open getter bool Moving

6:

if(_tt.Fitted)

7:

return _tt.IsEnabled;

8:

else

9:

return false;

10:

endif

11:

endgetter

12:
13:

// x-coordinate of the center.

14:

open getter int X

15:

return _x.Clone(); // Encapsulation.

16:

endgetter

17:

open setter X(int value)

18:

_x.Value = value; // Encapsulation.

19:

endsetter

20:

// y-coordinate of the center.

21:

open getter int Y

22:

return _y.Clone(); // Encapsulation.

23:

endgetter

24:

open setter Y(int value)

25:

_y.Value = value; // Encapsulation.

26:

endsetter

_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...

endsetter


The setter definition is the definition that starts with access control and the 'setter' keyword. And, it ends with 'endsetter' keyword.

Access control

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.

setter keyword

The 'setter' keyword indicates that this definition is a setter definition. It must be specified at the next of the access-control.

Setter name

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.

A setter of the same name that has different argument can be defined.

e.g.


open setter X(long value)

_x.Value = value.I; // Cast and encapsulation.

endsetter


Setter name 'X' already exists. However, the program will operate normally even if the new setter be added to Circle.88.

Argument

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.

e.g.

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.

endsetter keyword

The 'endsetter' keyword indicates the end of the setter definition.

Next
Previous
Copyright © Rice All rights reserved.