// Allocate and configure a Car object.
Car myCar = new Car();
myCar.petName = "Henry";
myCar.currSpeed = 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();
}
Once you run your program, you will see that the Car object (myCar) maintains its current state
throughout the life of the application, as shown in Figure 5-2.
Figure 5-2.
Taking the Car for a test drive (pun intended)
Allocating Objects with the new Keyword
As shown in the previous code example, objects must be allocated into memory using the new key-
word. If you do not make use of the new keyword and attempt to make use of your class variable in a
subsequent code statement, you will receive a compiler error:
static void Main(string[] args)
{
Do'stlaringiz bilan baham: |