Game Development Reference
In-Depth Information
in different subclasses: motorized or not motorized. You could choose to do this the other way
around. For some classes, it isn't entirely clear where in the hierarchy they belong: do you say that
a motorbike is a special type of bike (one with a motor)? Or is it a special kind of motorized vehicle
(one with only two wheels)?
What is important is that the relationship between the classes themselves is clear. A sailboat is a
boat, but a boat isn't always a sailboat. A bicycle is a vehicle, but not every vehicle is a bicycle.
Values vs. References
Before you finish this chapter, let's see how objects and variables are dealt with in memory. When
dealing with basic types such as numbers or Booleans, the variables are directly associated with a
place in memory. For example, look at the following declaration and initialization:
var i = 12;
After this instruction has been executed, the memory looks as depicted in Figure 11-5 .
i
12
Figure 11-5. Memory usage of a number variable
Now you can create a new variable j and store the value of variable i in that variable:
var j = i;
Figure 11-6 shows what the memory looks like after executing this instruction.
i
12
j
12
Figure 11-6. Memory usage after declaring and initializing two number variables
If you assign another value to the j variable, for example by executing the instruction j = 24 , the
resulting memory usage is shown in Figure 11-7 .
i
12
j
24
Figure 11-7. Memory usage after changing the value of the j variable
Now let's look at what happens when you use variables of a more complicated type, such as the
Cannon class. Consider the following code:
var cannon1 = new Cannon();
var cannon2 = cannon1;
 
Search WWH ::




Custom Search