When you declare a variable of a class, a special
method must be called to initialize the members of that class. This method
is automatically provided for every class and it is called a constructor.
Whenever you create a new class, a constructor is automatically provided
to it. This particular constructor is called the default
constructor. You have the option of creating it or not. Although a constructor is created for your class, you can
customize its behavior or change it as you see fit.
The constructor of a class is called New and it is
created as a sub procedure. Here is an example:
<script language="vb" type="text/vb" runat="server">
Public Class Square
Public Sub New()
End Sub
End Class
</script>
Like every method, a constructor is equipped with
a body. In this body, you can access any of the member variables (or
method(s)) of the same class. Consider the following program:
<%@ Page Language="VB" %> <html> <head> <script language="vb" type="text/vb" runat="server"> Public Class Square Public Sub New() End Sub End Class </script> <title>Exercise</title> </head> <body> <% Dim sq As Square = New Square %> </body> </html>
When a class has been instantiated,
its constructor is the first method to be called. For this reason, you can
use a constructor to initialize a class; that is, to assign default values
to its member variables. Based on this, instead of initializing the member
variable(s) of a class when initializing it or them, or instead of
creating a special method used to initialize the member variable(s) of a
class, you can use a constructor to do this. The advantage of a
constructor is that it doesn't need to be called: it is automatically
available whenever the class is instantiated.
In the previous section, we saw that there was always a
default constructor for a new class that you create. You just have the option of
explicitly creating one or not. The default constructor as we saw it doesn't
take arguments. Instead of a default
constructor, you may want to create a constructor that takes an argument. Here
is an example:
<script language="vb" type="text/vb" runat="server">
Private Class Square
Public Sub New(ByVal sd As Double)
End Sub
End Class
</script>
With this type of constructor, when you declare an instance
of the class, you can use this new constructor to initialize the class.
If
you create one constructor for your class and pass at
least one argument to that constructor, the automatic default
constructor disappears. This implies that if you declare an instance
of the class and use the default constructor to initialize it, you would
receive
an error when the web page displays.
If you still want to use the default constructor in a class
after creating a constructor that takes at least one argument, you must
explicitly create that default constructor.
A constructor is the primary method of a class. It allows
the programmer to initialize a variable of a class when the class is
instantiated. A constructor that plays this role of initializing an instance of
a class is also called an instance constructor. Most of the time, you don't
need to create a constructor, since one is automatically provided to any class
you create. Sometimes though, as we have seen in some classes, you need to create
your own constructor as you judge it necessary and sometimes, a single constructor
may not be sufficient. For example, when creating a class, you may decide, or
find out, that there must be more than one way for a user to initialize a
variable.
Like any other method, a constructor can be overloaded. In
other words, you can create a class and give it more than one constructor. The
same rules used on overloading regular methods also apply to constructors: the
different constructors must have different number of arguments or different
types of arguments.
|
Class Construction and Destruction
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment