Java Reference
In-Depth Information
How It Works
You shouldn't have any difficulty with this class definition, as it is very straightforward. The Line class
stores two Point objects as instance variables. There are two constructors for Line objects — one ac-
cepting two Point objects as arguments and the other accepting the ( x,y ) coordinates of the start and end
points. You can see how you use the variable this to differentiate the class instance variables, start and
end , from the parameter names in the constructor.
Note how the constructor that accepts Point objects works:
// Create a line from two points
Line(final Point start, final Point end) {
this.start = new Point(start);
this.end = new Point(end);
}
With this implementation of the constructor, two new Point objects are created that are identical to, but
independent of, the objects passed to the constructor. If you don't think about what happens, you might
be tempted to write it as:
// Create a line from two points - a poor implementation!
Line(final Point start, final Point end) {
this.start = start; // Dependent on external
object!!!
this.end = end; // Dependent on external
object!!!
}
The important thing to note here is that the way the constructor is implemented could cause problems
that might be hard to track down. In this version of the constructor no new points are created. The start
and end members of the object refer to the Point objects that are passed as arguments. The Line ob-
ject is implicitly dependent on the Point objects that are used to define it. If these were changed outside
the Line class, by using the move() method, for example, this would “silently” modify the Line object.
You might consciously decide that this is what you want, so the Line object continues to be dependent
on its associated Point objects. The rationale for this in a drawing package, for example, might be that
this would allow a point to be moved, and all lines based on the point would also be moved accordingly.
However, this is different from allowing such interdependencies by accident. In general, you should take
care to avoid creating implicit dependencies between objects unless they are what you intended.
In the toString() method for the Line class, you are able to use the Point objects directly in the form-
ation of the String representation of a Line object. This works because the Point class also defines a
toString() method.
You've now defined two classes. In these class definitions, you've included the basic data that defines an
object of each class type. You've also defined some useful methods for operating on objects, and added
constructors for a variety of input parameters. Note how the Point class is used in the definition of the
Line class. It is quite natural to define a line in terms of two Point objects, and the Line class is much
simpler and more understandable than if it were defined entirely in terms of the individual x and y co-
ordinates. To further demonstrate how classes can interact, and how you can solve problems directly, in
terms of the objects involved, let's devise a method to calculate the intersection of two Line objects.
Search WWH ::




Custom Search