Home News DOC Class DL Others Cooker
Documents  >  Tutorial  >  Getters
Getter definition

A getter is an interface that uses the form of variable reference. The main purpose is to provide access to the fields.


Table of contents:


Format

Access control

getter

Return type

Getter name

endgetter

Example

Format

The format of the getter definition is:


Access-control getter Return-type Getter-name

...

endgetter


It is a definition that starts with access control, followed by the keyword getter, and ends with the keyword endgetter.

You can define getters with different names and place anywhere in the class definition.

The getter definition will generate a scope.

Access control

It specifies the access level of the getter using the keywords open or closed.

If you specify open, the getter can be called from outside of the class definition.

If you specify closed, the getter can only be used inside of the class definition.

getter

It is a keyword that indicates that it is a getter definition. It must be specified next to access control.

Return type

It specifies the return type after the keyword getter. This indicates the class of instances returned by the getter.

It uses the return statement to return a value. The class of this value must match the return type.

Getter name

It specifies the getter name after the return type. The naming rules for getter name is the same as for identifiers.

Exceptionally, you can use the name of the built-in class as the getter name.

endgetter

It is a keyword that indicates the end of the getter definition.

You can use the abbreviation "eg" instead of "endgetter". Note that "eg" is also a keyword.

Example

Let's define getters in the example class.

1:

class example

2:

int x;

3:

int y;

4:

open fitter example() // Default fitter

5:

x = 0;

6:

y = 0;

7:

endfitter

8:

open fitter example(int xx, int yy) // Fitter with two arguments.

9:

x = xx;

10:

y = yy;

11:

ef // Abbreviation.

12:

open setter X(int val)

13:

x = val;

14:

endsetter

15:

open getter int X

16:

return x;

17:

endgetter

18:

open setter Y(int val)

19:

y = val;

20:

es // Abbreviation.

21:

open getter int Y

22:

return y;

23:

eg // Abbreviation.

24:

endclass

It calls a getter using the form of a variable reference.

1:

example ex = new example(10, 10);

2:

int cx = ex.X; // open getter int X

3:

int cy = ex.Y; // open getter int Y

Next
Previous
Copyright © Rice All rights reserved.