Multiple Objects


You have learned that a class is a kind of stencil or cookie cutter for creating objects. Cookie cutters are especially useful if you're going to make lots of cookies. Similarly, classes are really useful because you can use a class to make multiple instances of that class. Different cookies made from the same cutter have the same outline, but they can be frosted or decorated differently. Similarly, different instances of the same class can have different data values.

Figure 7.4 shows three instances of the Person class. Each instance is referred to by a different reference.

click to expand
Figure 7.4: Multiple objects

The data values inside an object can be manipulated in all the same ways you can manipulate ordinary variables. For example, if curly and larry are references to Person objects, you might write the following:

curly.age = larry.age + 12;

The SeveralObjectsLab animated illustration lets you play with multiple instances of the Person class. Start the program by typing java objects.SeveralObjectsLab. You will see the display shown in Figure 7.5.

click to expand
Figure 7.5: SeveralObjectsLab

SeveralObjectsLab initially displays the following code:

Person reference1 = new Person(); Person reference2 = new Person(); Person reference3 = new Person(); reference1.age = 30; reference1.age = 30; reference1.age = 30; reference1.age = 30; reference1.age = 30;

The reference variable names aren't very imaginative. Also, it isn't very useful to have five lines that all set the same field in the same object to the same value.

That's where you come in. Type better names into the text fields in the declaration lines. Use the choices to reference different fields in different objects. Use the text fields in the assignment lines to assign any value you like to the fields you have chosen. The assignment values can be literals or expressions, and the expressions can include fields in any of the objects. Figure 7.6 shows SeveralObjectsLab after reconfiguring.

click to expand
Figure 7.6: SeveralObjectsLab reconfigured

Figure 7.7 shows the result of executing Figure 7.7.

click to expand
Figure 7.7: SeveralObjectsLab reconfigured and executed

Try configuring SeveralObjectsLab to execute the following code:

Person simon = new Person(); Person emily = new Person(); Person bethan = new Person(); simon.age = 30; emily.age = simon.age - 20; simon.weight = 150; bethan.weight = simon.weight / 3; bethan.age = bethan.weight / 5;

The program demonstrates construction of the objects and references, followed by assignment to the various fields. Try configuring different values. Hopefully, the results will not be surprising.

The point of SeveralObjectsLab is to get you to think of objects as bundles of data. Objects are similar to other instances of the same class, to the extent that all such objects contain similar clusters of data. That is, each cluster has the same number of variables, and those variables have the same types and names, as defined in the class definition file. However, each object is distinct and has its own version of each variable defined by the class.




Ground-Up Java
Ground-Up Java
ISBN: 0782141900
EAN: 2147483647
Year: 2005
Pages: 157
Authors: Philip Heller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net