CHAPTER 5 UNDERSTANDING ENCAPSULATION
public void SpeedUp(int delta)
{
currSpeed += delta;
}
}
PrintState() is more or less a diagnostic function that will simply dump the current state of a given
Car object to the command window. SpeedUp() will increase the speed of the Car by the amount specified
by the incoming int parameter. Now, update your Main() method in the Program class with the following
code:
static void Main(string[] args)
{
Console.WriteLine("***** Fun with Class Types *****\n");
// Allocate and
Car myCar = new
myCar.petName =
myCar.currSpeed
}
configure a Car object.
Car();
"Henry";
= 10;
// Speed up the car a few times and print out the
// new state.
for (int i = 0; i <= 10; i++)
{
myCar.SpeedUp(5);
myCar.PrintState();
}
Console.ReadLine();
After you run your program, you will see that the Car variable (myCar) maintains its current state
throughout the life of the application, as shown in the following code:
***** Fun with Class Types *****
Henry
Henry
Henry
Henry
Henry
Henry
Henry
Henry
Henry
Henry
Henry
is
is
is
is
is
is
is
is
is
is
is
going
going
going
going
going
going
going
going
going
going
going
15
20
25
30
35
40
45
50
55
60
65
MPH.
MPH.
MPH.
MPH.
MPH.
MPH.
MPH.
MPH.
MPH.
MPH.
MPH.
165