by kirupa |
5 January
2007
In the
previous page I introduced a lot of terminology and
helped explain what classes and objects are by using a
planetary example.
In this tutorial, you will be writing some code to put into
practice what you read. While you can use any IDE, I will
provide instructions for what most of you probably use for
your .NET development, Visual Studio 2005:
- Launch Visual Studio. Once launched, go to File |
New | Project:
[ create a new project by going to File | New | Project ]
- The New Project window should appear. From
this window, select Console Application under the
Templates view:
[ create a new Console Application ]
- Glance your eyes down a few inches and
enter ClassTutorial as the name for
your project:
[ call your new project ClassTutorial ]
- Press OK to accept the changes.
You now have a empty project with a Main method that you
can use to test your code in.
Let's now take a look at defining a class. Copy and paste
this code directly above your class
Program definition in the code view:
- class
Planet
- {
- public
int
radius;
- public
int
gravity;
- private
string
name;
- }
For clarification on where exactly to paste the code, my
entire code view looks like the following after pasting the above
code:
- using
System;
- using
System.Text;
-
- namespace
ClassTutorial
- {
- class
Planet
- {
- public
int
radius;
- public
int
gravity;
- private
string
name;
- }
-
- class
Program
- {
- static
void
Main(string[]
args)
- {
-
- }
- }
- }
Let's get back the Planet class you copied and pasted
earlier. In the first line I use the class keyword followed
by the name of our class. It is good programming convention
to capitalize the first letter of a class.
The body of our class contains three fields (variables):
radius, gravity, name. Any object that is based on the
planet class will have a copy of these three variables and
any values you assign to these variables.
If you wanted to use our Planet class, all you need to do
is create an object of type
Planet:
- Planet
earth
= new
Planet();
- earth.gravity
= 9.81;
- earth.radius
= 6378;
Notice that I use the new
keyword to initialize earth to refer to an object
of type Planet. With our earth object created, we can
easily assign values to the variables radius and gravity as
shown above. If you attempt to assign a value to the name field
using earth.name,
you will receive an error. The reason is that name is a
private field whereas radius and gravity
are both public fields.
A public field is one in which you can access the values
from outside of the class. For example, an object can access
something marked as public as you saw when we assigned
values to our gravity and radius fields.
When something is marked private, though, that means only
code stored within our class can access it. Earlier, I said
that you couldn't access the name field from our earth
object. That is because our earth object is beyond the reach
of our private name field, so we couldn't access the
name field and assign it a
name like we did for our public
gravity and radius
fields.
When you create an object using the
new keyword, you use something called a
constructor. A constructor is usually a method (function)
whose name is same as that of our class, and it often takes
in arguments that help initialize any fields that the object
would need.
In our Planet class, we do not have a constructor defined. When
you do not define a constructor, a default constructor is
provided for you. That is why when you typed
Planet earth = new Planet(),
your compiler did not give you an error.
So, let's modify our Planet class by creating a
constructor:
- class
Planet
- {
- public
int
radius;
- public
double
gravity;
- private
string
name;
-
- // A Constructor
- public
Planet()
- {
- Console.WriteLine("Constructor
called!");
- }
- }
In the above code example, notice that I created a simple
constructor called Planet(). This constructor does pretty
much the same thing as our automatically generated default
constructor, but one difference is that in my version, I
output "Constructor called!" to the console everytime a new
Planet object is created.
A default constructor is not fun. After all, a primary
reason for using a constructor is so that your objects can
have their fields initialized when they are created. A default
constructor doesn't help with that unless you have no fields
that need initializing. Since we have three fields that need
to be initialized, let's modify our Planet constructor
again:
- class
Planet
- {
- public
int
radius;
- public
double
gravity;
- private
string
name;
-
- // A Constructor
- public
Planet(int
r,
int
g,
string
n)
- {
- radius
=
r;
- gravity
=
g;
- name
=
n;
- }
- }
Notice that our constructor takes in three arguments this
time. In the body of the constructor, I assign our
radius,
gravity, and
name fields the value of the
incoming r,
g, and
n arguments.
To use the above constructor, you will use the following
line:
- Planet
earth
= new
Planet(6378,
9.81,
"Earth");
Notice that our earlier no-argument
new Planet() constructor
will no longer work. Since your constructor now takes in
three arguments, you must provide the three arguments needed
or else you won't be able to create any new objects of type
Planet.
But notice that, since you pass in the values you want
your fields to have, you no longer need to initialize them
separately. If you want to know what values your earth
object's radius and gravity variables possess, all you have
to is type Console.WriteLine(earth.radius
+ ", " + earth.gravity);
Despite our constructor simplifying how fields are
initialized, we still cannot access our private name field
outside of the Planet class. Notice, though, that our
constructor was able to assign the value "Earth"
to our name field because the constructor is inside the
class itself. Hopefully before the next section is over, we
will figure out a way to access the data stored by our
name variable!
Onwards to the
next
page!
|