Game Development Reference
As soon as the ?heajp requests information from the ceraIkjauPk?heajp cap method, the method
first runs an eb statement to check which value should be returned: pnqa or b]hoa. (Using an eb
statement is optional, but this sort of checking makes using cap methods particularly useful). The
eb statement checks whether the client has any money in her account, and returns the appropriate value.
Figure 8-3 illustrates the new improved relationship between the >]jg and the ?heajp.
The Client Class
[d]oIkjau9>]jg* ceraIkjauPk?heajp 7
The Bank Class
lq^he_op]pe_bqj_pekjcap ceraIkjauPk?heajp $%6>kkha]j
1. The Client sends the
Bank a request for money.
2.The Bank figures out
whether the Client has
money, which in this
case is false.
3. The Bank sends false back to the Client. The Client's
_hasMoney property now has the value of false.
Figure 8-3. Using a getter allows the Bank to check the request and send back only the correct information.
cap methods look and behave like variables, but they can be programmed with conditions attached.
They look like ordinary function definitions, but include the keyword cap after the keyword bqj_pekj.
Here's a general format for using them:
They can include any code but always return a single value (they send some information back to what-
ever object was requesting it).
Although the other methods and properties in the class are lner]pa, cap methods are always defined
as lq^he_ so that other classes can access them.
The keyword napqnj is used to send the value back. This sends pnqa back to the ?heajp class:
This sends b]hoa back to the ?heajp class: