Classes, Objects, Methods, Properties and Instance Variables

Let's 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 it. 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 drive a car easily.

Unfortunately, you cannot drive the engineering drawings of a car. Before you can drive a car, the car must be built from the engineering drawings that describe it. A completed car will have an actual accelerator pedal to make the car go faster, but even that's 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 an application requires a method. The method describes the mechanisms that actually perform its tasks. The method hides from its user the complex tasks that it performs, just as the accelerator pedal of a car hides from the driver the complex mechanisms of making the car go faster. In C#, we begin by creating an application unit called a class to house (among other things) a method, just as a car's engineering drawings house (among other things) 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 one method to deposit money in an account, another to withdraw money from an account and a third to inquire what the current account 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 a car, you must build an object of a class before you can get an application to perform the tasks the class describes. That is one reason C# 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 taskmake the car go faster. Similarly, you send messages to an objecteach message is known as a method call and tells a method of the object 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 total miles driven (i.e., its odometer reading). Like the car's capabilities, these 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. Every car maintains its own attributes. 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 the object as it is used in an application. These attributes are specified as part of 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.

Notice that these attributes are not necessarily accessible directly. The car manufacturer does not want drivers to take apart the car's engine to observe the amount of gas in its tank. Instead, the driver can check the meter on the dashboard. The bank does not want its customers to walk into the vault to count the amount of money in an account. Instead, the customers talk to a bank teller. Similarly, you do not need to have access to an object's instance variables in order to use them. You can use the properties of an object. Properties contain get accessors for reading the values of variables, and set accessors for storing values into them.

The remainder of this chapter presents examples that demonstrate the concepts we introduced here in the context of the car analogy. The first four examples, summarized below, 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 that 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 by displaying the name as part of the welcome message.
  3. The third example shows how to store the course name in a GradeBook object. For this version of the class, we also show how to use properties to set the course name and obtain the course name.
  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.

The last example in the chapter presents an Account class that reinforces the concepts presented in the first four examples and introduces the decimal typea decimal number can contain a decimal point, as in 0.0345, 7.23 and 100.7, and is used for precise calculations, especially those involving monetary values. For this purpose, we present an Account class that represents a bank account and maintains its decimal balance. The class contains a method to credit a deposit to the account, thus increasing the balance, and a property to retrieve the balance and ensure that all values assigned to the balance are non-negative. The class's constructor initializes the balance of each Account object as the object is created. We create two Account objects and make deposits into each to show that each object maintains its own balance. The example also demonstrates how to input and display decimal numbers.

Категории