Home News DOC Class DL Others Cooker
Documents  >  Tutorial  >  Setters
Setter definition

A setter is an interface that uses the form of assignment. The main purpose is to provide access to the fields.


Table of contents:


Format

Access control

setter

Setter name

Argument

endsetter

Example

Format

The format of the setter definition is:


Access-control setter Setter-name(Argument)

...

endsetter


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

You can define setters with different argument classes even if they have a same name. And, you can place them anywhere in the class definition.

An assignment to setter is automatically converted to a setter call.

The setter definition will generate a scope.

Access control

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

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

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

setter

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

Setter name

It specifies the setter name after "setter". The naming rules for setter name is the same as for identifiers.

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

Argument

Seeter argument is specified next to the Setter-name, it must be enclosed by parentheses.

An argument is a pair of class name and argument name. One argument is required for the setter. The argument class is the class of the value that can be assigned to the setter.

Since the setters are also distinguished in the argument class, you can define setters with the same name but different argument classes.

endsetter

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

You can use the abbreviation "es" instead of "endsetter". Note that "es" is also a keyword.

Example

Let's define setters 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 setter Y(int val)

16:

y = val;

17:

es // Abbreviation.

18:

endclass

The setter is called by an assignment expression.

1:

example ex = new example();

2:

ex.X = 10; // open setter X(int val)

3:

ex.Y = 10; // open setter Y(int val)

Next
Previous
Copyright © Rice All rights reserved.