Game Development Reference
In-Depth Information
Fig. 7.2 The two roles of the
'class' concept
variable that is of its type. To try this, add the following member variable declaration
to the Painter class, just below the large list of existing variables:
Cannon cannon;
And add the following line at the end of the LoadContent method:
cannon = new Cannon();
We have now created an instance of our own class! Of course, since this class is
empty, the cannon object that we have created does not have any data, properties or
behavior yet. We are going to have to add this ourselves.
7.3.4 Classes Can Be Used as a Type
Earlier in this topic, we defined the term 'class' as a group of methods with a name.
A method should always belong to a class: the Draw method for example belongs to
the SpriteBatch class. However, classes also fulfill another role: they are the type of
objects. In that case, the name of the class is used as the type of a variable, just like
you can use built-in types such as int :
int x; Cannon c;
Both of these roles that a class plays are strongly connected. After all, methods
change an object (the object before the dot in a method call). This object consists of
variables that can be changed by the instructions in the method. Objects that have a
class as a type can be modified by methods from that class. In other words: methods
of a class can change objects that have that class as a type. Figure 7.2 shows the
relation between the concepts instruction, variable, method, object and class, where
the double role of the class is clearly visible.
 
Search WWH ::




Custom Search