Game Development Reference
In-Depth Information
In this example, the client is sending the bank's `alkoep setter the value of its [_da_gR]hqa (which is
-,,). The setter first checks to make sure that it's actually being sent a value that would be appropri-
ate to deposit. (Hey, these clients are sneaky, it has to check everything!) If it seems okay, the value
is deposited into the bank's [_heajp=__kqjpR]hqa property. Like getters, setters are also defined as
lq^he_.
Figure 8-4 illustrates this relationship.
2.The Bank temporarily
stores 100 in a local
variable called money.
The Client Class
The Bank Class
lq^he__h]oo?heajp
w
lner]par]n[_da_gR]hqa9-,,7
lq^he_bqj_pekj?heajp$%
w
>]jg* `alkoep 9[_da_gR]hqa7
y
y
lq^he__h]oo>]jg
w
lner]par]n[_heajp=__kqjpR]hqa9,7
1. The Client sends the
Bank's setter the value
of _checkValue, which
is 100.
lq^he_op]pe_bqj_pekjoap `alkoep $ikjau6ejp%6rke`
w
eb$ikjau:,%
w
[_heajp=__kqjpR]hqa9ikjau7
y
y
y
3. If the amount seems acceptable, the
Bank stores it in a private property called
_clientAccountValue. Its value is now 100.
Figure 8-4. Setters allow classes to receive information from other classes and use that information to change
their properties if the conditions seem fair.
The wonderful thing about using getters and setters is that objects can communicate without having
to directly access or change each other's properties. This helps with encapsulation, which means that
the classes are completely sealed off from one another. If they have to communicate, they do so only
under strict conditions. If you use getters and setters in your own classes, as in these examples, you're
far less likely to encounter bugs that come from objects not being able to make sense of information
they weren't programmed to deal with.
Getting started with the object-oriented approach
All you really need to know to get started using object-oriented programming are the techniques and
concepts discussed over the last few pages:
Think of each class as a model of an object. Describe the object using properties and
methods.
Make all your properties and methods private. If ?h]oo= tries to directly change or access the
properties in ?h]oo>, you'll begin to weave a tangled web of dependencies that could be very
difficult to debug if things go wrong.
Your classes should communicate with each other using getters and setters.
 
Search Nedrilad ::




Custom Search