Details of
Object-Oriented Implementation
Specifying the Class
-
The
class specifier starts with a member access modifier,
then the keyword Class,
followed by the class name.
-
By convention, begin class names with a capital letter and use a capital letter for every word in the class
name (SampleClassName).
Access
modifier
-
The
member access modifier can be Public,
Private, Protected,
Friend,
or Protected Friend.
-
A key feature of object-oriented programming
is data hiding,
which means that data is concealed within
a class, so that it cannot be accessed mistakenly by functions
outside the class.
-
The primary mechanism for hiding data is to put
it in a class and make it Private.
-
Private variables or
methods are accessible only to members of the class.
-
Public data or
methods are accessible from both inside and outside the class.
Anyone can access the data or methods and possibly alter it.
-
Protected
is a special modifier used in inheritance.
-
Friend
variables or
methods are
accessible only within the program that contains the entity declaration.
-
Protected Friend
variables or
methods have the union of Protected
and Friend
accessibility.
-
Classes
that do not specify an access modifier are declared as
Friend
by default.
-
If
the access modifier of an instance variable or constant is not explicitly
declared inside a class, it defaults to
Private.
-
If
the access modifier of a method is not explicitly declared inside a class, it
defaults to Public.
Calling Methods
-
Public methods
can be accessed from outside the class.
-
Usually the data within a class is
Private and
the methods are Public.
-
To use a method, the dot operator (the period)
connects the object name and the method: demoObj.setVar(1066)
-
Methods of a class can be accessed only by an
object of that class.
Constructor
(Sub New)
-
A
constructor is a member sub (method) that has the name
New.
-
The
constructor is executed automatically whenever an object is instantiated
from that class.
-
Constructors
are used to initialize the instance variables of a class.
-
Constructors
cannot specify return types or return values.
Public
Sub New ( <optional parameter list> )
' initialization of instance variables or
other instantiation tasks
End Sub
-
Arguments
are sometimes passed to constructors -- these arguments provide values
that the constructor uses when initializing the instance variables.
-
If
a constructor is not defined explicitly in a class, then an implicit
no-argument constructor is provided by the IDE.
-
If you don't explicitly declare a no-arguments constructor but declare some other constructor, you will only get the constructor that you
do explicitly define. If you never declared a no-arguments constructor but do define another type of constructor,
VB.NET does not insert a no-arguments constructor.
-
The
default constructor initializes the primitive numeric data type
variables to 0, Boolean variables to false and references to null.
-
Multiple
constructors can be specified for a single class, that is, constructors may
be overloaded to provide a variety of means for initializing objects of a
class.
Instantiation
-
Object declaration:
Dim
objectName As ClassName
-
Allocation: objectName = New
ClassName( )
-
All-in-one:
Dim
objectName As ClassName
= New ClassName( )
Summary
One benefit of the
object-oriented approach is the close correspondence between
the real-world things being modeled by the program and the objects in the program.
This makes it easy to conceptualize a programming
problem. You figure out what parts of the problem can be most
usefully represented as objects, and then put all the data and methods connected with that object into the class specification.
In some situations it may not be obvious what parts
of a real-life situation should be made into objects because
there are no hard and fast rules. Often you proceed by trial and
error. You break the problem into objects in one way and write
trial class specifiers for these objects. If the classes seem
to match reality in a useful way, you continue. If they don't,
you start over, selecting different entities to be classes. The
more experience you have with OOP, the easier it will be to break
a programming problem into classes.
Some of the benefits of object-oriented programming
are probably not apparent at this point because OOP was devised
to cope with the complexity of large programs. Smaller programs
have less need for the organizational power that OOP provides.
The larger the program, the greater the benefit.
For
an example, click here.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vastmClass.asp
|