Section 4.2. Classes, Objects, Methods and Instance Variables


4.2. Classes, Objects, Methods and Instance Variables

We will begin with a simple analogy to help you understand classes and their contents. Suppose you want to drive a car and make it go faster by pressing down on its accelerator pedal. What must happen before you can do this? Well, before you can drive a car, someone has to design the car. A car typically begins as engineering drawings, similar to the blueprints used to design a house. These engineering drawings include the design for an accelerator pedal to make the car go faster. The pedal "hides" the complex mechanisms that actually make the car go faster, just as the brake pedal "hides" the mechanisms that slow the car and the steering wheel "hides" the mechanisms that turn the car. This enables people with little or no knowledge of how engines work to easily drive a car.

Unfortunately, you cannot drive a car's engineering drawings. Before you can drive a car, the car must be built from the engineering drawings that describe it. A completed car has an actual accelerator pedal to make it go faster, but even that is not enoughthe car will not accelerate on its own, so the driver must press the accelerator pedal.

Now let's use our car example to introduce the key programming concepts of this section. Performing a task in a program requires a method. The method describes the mechanisms that actually perform its tasks. The method hides these mechanisms from its user, just as the accelerator pedal of a car hides from the driver the complex mechanisms that make the car go faster. In Visual Basic, we begin by creating a program unit called a class to house a method, just as a car's engineering drawings house the design of an accelerator pedal. In a class, you provide one or more methods that are designed to perform the class's tasks. For example, a class that represents a bank account might contain many methods, including one to deposit money in the account, another to withdraw money from the account and a third to inquire what the current balance is.

Just as you cannot drive an engineering drawing of a car, you cannot "drive" a class. Just as someone has to build a car from its engineering drawings before you can actually drive it, you must build an object of a class before you can get a program to perform the tasks the class describes how to do. That is one reason Visual Basic is known as an object-oriented programming language.

When you drive a car, pressing its gas pedal sends a message to the car to perform a taskthat is, to go faster. Similarly, you send messages to an objecteach message is a method call and tells one of an object's methods to perform its task.

Thus far, we have used the car analogy to introduce classes, objects and methods. In addition to the capabilities a car provides, it also has many attributes, such as its color, the number of doors, the amount of gas in its tank, its current speed and its odometer reading (i.e., its total miles driven since it was built). Like the car's capabilities, its attributes are represented as part of a car's design in its engineering diagrams. As you drive a car, these attributes are always associated with the car. For example, each car knows how much gas is in its own gas tank, but not how much is in the tanks of other cars. Similarly, an object has attributes that are carried with it as it's used in a program. These attributes are specified in the object's class. For example, a bank account object has a balance attribute that represents the amount of money in the account. Each bank account object knows the balance in the account it represents, but not the balances of the other accounts in the bank. Attributes are specified by the class's instance variables.

This chapter presents five simple examples that demonstrate the concepts introduced in the context of the car analogy. The examples incrementally build a GradeBook class:

  1. The first example presents a GradeBook class with one method that simply displays a welcome message when it is called. We show how to create an object of the class and call the method so that it displays the welcome message.

  2. The second example modifies the first by allowing the method to receive a course name as an argument and display the name as part of the welcome message.

  3. The third example shows how to store the course name in a GradeBook object. We show how to use the convenient notation of "properties" to set the course name in the object and obtain the course name from the object.

  4. The fourth example demonstrates how the data in a GradeBook object can be initialized when the object is createdthe initialization is performed by the class's constructor (a special type of method with the name New).

  5. The last example enhances class GradeBook by introducing data validation, which ensures that data in an object adheres to a particular format or is in a proper value range. For example, a Date object should accept month values only in the range 112. In our GradeBook example, the property that sets the course name for a GradeBook object ensures that the course name is 25 or fewer characters. If not, the property uses only the first 25 characters of the course name and displays a warning message. Restrictions like this are common in information systems that have to display data in fixed-size boxes in a form on the screen.

The GradeBook examples in this chapter do not process or store grades. We begin processing grades with the version of class GradeBook in Chapter 5, Control Statements: Part 1, and we store grades in the version of GradeBook in Chapter 8, Arrays.




Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net