Java Reference
In-Depth Information
LISTING 7.7
continued
private
String streetAddress, city, state;
private
long
zipCode;
//-----------------------------------------------------------------
// Constructor: Sets up this address with the specified data.
//-----------------------------------------------------------------
public
Address (String street, String town, String st,
long
zip)
{
streetAddress = street;
city = town;
state = st;
zipCode = zip;
}
//-----------------------------------------------------------------
// Returns a description of this Address object.
//-----------------------------------------------------------------
public
String toString()
{
String result;
result = streetAddress + "\n";
result += city + ", " + state + " " + zipCode;
return
result;
}
}
The more complex an object, the more likely it will need to be represented as
an aggregate object. In UML, aggregation is represented by a connection between
two classes, with an open diamond at the end near the class that is the aggregate.
Figure 7.2 shows a UML class diagram for the
StudentBody
program.
Note that in previous UML diagram examples and in Figure 7.2, strings are not
represented as separate classes with aggregation relationships, though technically
they could be. Strings are so fundamental to programming that often they are
represented as though they were a primitive type in a UML diagram.
Before we leave the topic of relationships among classes, we should examine
another special reference used in Java programs called the
this
reference. The
word
this
is a reserved word in Java. It allows an object to refer to itself. As we
have discussed, a nonstatic method is invoked through (or by) a particular object
Search WWH ::
Custom Search