Home About Rice Downloads Documents Others Cooker CookerGX
Documents  >  Tutorial  >  getters
Getter

Purpose of this chapter.

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

Getter

The getter is the interface to call the class member. It use the format of the reference to a variable. The main purpose is to provide the ability to access field.

First, let us look at the getter 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

if(_currentCircle.Moving) // Sample.cookの102行目

The reference to the getter name 'Moving' will be converted to the call of the 'open getter bool Moving'.


A definition of the getter is the following format.


access-control(open or closed) getter return-value-type getter-name

Statements of the getter definition...

endgetter


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

Access control

The access level of the getter will be specified using the 'open' or 'closed' keyword.

If the 'open' keyword is specified, the getter can be used anywhere in the program.

If the 'closed' keyword is specified, the getter 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.

getter keyword

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

Return value type

A type of the return value must be specified at the next of the 'getter' keyword. It indicates a class name of an instance that will be returned from a getter.

Getter name

The getter-name must specify at the next of the return-value-type. The naming rule of the getter name is the same as the identifier.

endgetter keyword

The 'endgetter' keyword indicates the end of the getter definition.

Next
Previous
Copyright © Rice All rights reserved.