Game Development Reference
In-Depth Information
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
lq^he__h]oo?heajp
w
lner]par]n[jaa`oPkL]u>ehho9pnqa7
lner]par]n[d]oIkjau9b]hoa7
lq^he_bqj_pekj?heajp$%
w
[d]oIkjau9>]jg* ceraIkjauPk?heajp 7
y
y
The Bank Class
lq^he__h]oo>]jg
w
lner]par]n[_heajpD]oIkjauEj=__kqjp9b]hoa7
lq^he_op]pe_bqj_pekjcap ceraIkjauPk?heajp $%6>kkha]j
w
eb$[_heajpD]oIkjauEj=__kqjp%
w
napqnjpnqa7
y
ahoa
w
napqnjb]hoa7
y
y
y
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:
lq^he_bqj_pekjcapiapdk`J]ia$%6NapqnjPula
w
napqnjokiaR]hqa7
y
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:
napqnjpnqa7
This sends b]hoa back to the ?heajp class:
napqnjb]hoa7
 
Search Nedrilad ::




Custom Search