// Error! Nonnested types cannot be marked private!
private class SportsCar
{}
The First Pillar: C#’s Encapsulation Services
The concept of encapsulation revolves around the notion that an object’s internal data should not
be directly accessible from an object instance. Rather, if the caller wants to alter the state of an
object, the user does so indirectly using accessor (i.e., “getter”) and mutator (i.e., “setter”) methods.
In C#, encapsulation is enforced at the syntactic level using the public, private, internal, and
protected keywords. To illustrate the need for encapsulation services, assume you have created the
following class definition:
C H A P T E R 5
■
D E F I N I N G E N C A P S U L AT E D C L A S S T Y P E S
164
8849CH05.qxd 10/22/07 1:34 PM Page 164
Do'stlaringiz bilan baham: |