Area
and
Occupants
fields, which are encapsulated by
Building
.
Thus, it is possible for the
Building
class to perform this calculation on its own. Furthermore,
by adding this calculation to
Building
, you prevent each program that uses
Building
from
having to perform this calculation manually. This prevents the unnecessary duplication of
code. Finally, by adding a method to
Building
that computes the area-per-person, you are
enhancing its object-oriented structure by encapsulating the quantities that relate directly
to a building inside
Building
.
To add a method to
Building
, specify it within
Building
’s declaration. For example, the
following version of
Building
contains a method called
Do'stlaringiz bilan baham: |