Java Reference
In-Depth Information
17.
The keyword
this
refers to the object on which a method or constructor has been
called (sometimes called the “implicit parameter”). It is used to access or set the
object's field values, to call the object's methods, or to call one constructor from
another.
18.
// Constructs a Point object with the same x and y
// coordinates as the given Point.
public Point(Point p) {
this.x = p.x;
this.y = p.y;
}
Here is an alternative version:
// Constructs a Point object with the same x and y
// coordinates as the given Point.
public Point(Point p) {
this(p.x, p.y); // call the (int, int) constructor
}
19.
Abstraction is the ability to focus on a problem at a high level without worrying
about the minor details. Objects provide abstraction by giving us more powerful
pieces of data that have sophisticated behavior without having to manage and
manipulate the data directly.
20.
Items declared
public
may be seen and used from any class. Items declared
private
may be seen and used only from within their own classes. Objects' fields
should be declared
private
to provide encapsulation, so that external code can't
make unwanted direct modifications to the fields' values.
21.
To access private fields, create accessor methods that return their values. For
example, add a
getName
method to access the
name
field of an object.
22.
// Sets this Point's x coordinate to the given value.
public void setX(int newX) {
x = newX;
}
// Sets this Point's y coordinate to the given value.
public void setY(int newY) {
y = newY;
}
Search WWH ::
Custom Search