VBA builds the object using the class, it doesn’t look at the class any more
unless it wants to build another object. This differentiation between classes
and objects is important to remember because many information sources
confuse the two concepts. You need to keep them separate to better under-
stand how objects work. The sections that follow describe classes and
objects in detail.
Understanding object-oriented
programming concepts
At one time, developers had to worry about every variable, construct, and
step in their code. Procedural languages use step-by-step instructions to
tell the computer how to perform a task. Many developers continue to use
procedural languages because they find them easy to use and understand.
Object-oriented programming (OOP) hides implementation details from the
developer. All the developer needs to know is that an object accomplishes a
specific task; how the object performs the task is up to the object’s developer.
The act of hiding the working details of an object is called
encapsulation.
The idea behind using objects is that you don’t worry about how the informa-
tion you type gets turned into executable code. This concept may sound really
odd to anyone who’s used to working with a procedural language, but that’s
the way it is. I had similar difficulties when I moved from assembler and C
to C++. It took some time for me to learn that although the object code does
get translated into executable code in some way, the whole reason for using
objects is to create an
abstraction
so that you worry less about the actual
underlying code than you do about the task the object is supposed to per-
form. The object creator takes care of the internal workings of the object.
Using classes also benefits the developer because it’s possible to inherit all
of the features of another class.
Inheritance
is the act of creating a new class
based on the content of a parent class. For example, you might already have
a class called
Dog
and want to create a new class called
BorderCollie
.
Because a border collie is a kind of dog, you can inherit the feature of the
Dog
class into the new
BorderCollie
class.
Class theory can become quite complex, but you really don’t need to worry
about the complexities when working with VBA. You can find all kinds of
OOP topics online that discuss everything from the intricacies of good class
definition to whether OOP is really better than procedural code. If you really
want to know the low-level concepts behind OOP, one of the best places to
begin is Wikipedia (
http://en.wikipedia.org/wiki/Object-oriented_
programming
).
182
Do'stlaringiz bilan baham: