Working with objects
If you have experience with object-oriented programming, you'll find much of D's OOP support familiar. As such, I need to reiterate my warning from before: D is not C++, Java, or C#. Familiarity can help you pick some things up more quickly, but it can also lead you to take other things for granted. This section introduces D's OOP features.
Encapsulation with protection attributes
There are four levels of protection in D: public
, package
, private
, and protected
. The first three apply to classes, structs, and modules, but protected
only has meaning with classes. We'll examine it later when we talk about inheritance.
Public
Anything declared public
in a module, whether it's in module scope or as part of a class
or struct
declaration, is visible anywhere it's imported. With the exception of import declarations, all declarations in a module, class
or struct
are implicitly public
. Save the following as $LEARNINGD/chapter03/protection1.d
:
module protection1...