Java Reference
In-Depth Information
return (x >= getX() && x < getX()+getWidth()) &&
(y >= getY() && y < getY()+getHeight());
}
}
Listing3-3 ' s Rectangle classdemonstratesnestedsubclasses.Eachofthe Double
and Float static member classes subclass the abstract Rectangle class, providing
private floating-point or double precision floating-point fields, and overriding Rect-
angle 's abstract methods to return these fields' values as double s.
Rectangle isabstractbecauseitmakesnosensetoinstantiate thisclass.Because
italsomakesnosensetodirectly extend Rectangle withnewimplementations (the
Double and Float nested subclasses should be sufficient), its default constructor
is declared private . Instead, you must instantiate Rectangle.Float (to save
memory) or Rectangle.Double (when accuracy is required), as demonstrated by
Listing 3-4 .
Listing 3-4. Instantiating nested subclasses
class SMCDemo
{
public static void main(String[] args)
{
Rectangle r = new Rectangle.Double(10.0, 10.0, 20.0,
30.0);
System.out.println("x = "+r.getX());
System.out.println("y = "+r.getY());
System.out.println("width = "+r.getWidth());
System.out.println("height = "+r.getHeight());
System.out.println("contains(15.0,
15.0)
=
"+r.contains(15.0, 15.0));
System.out.println("contains(0.0,
0.0)
=
"+r.contains(0.0, 0.0));
System.out.println();
r = new Rectangle.Float(10.0f, 10.0f, 20.0f, 30.0f);
System.out.println("x = "+r.getX());
System.out.println("y = "+r.getY());
System.out.println("width = "+r.getWidth());
Search WWH ::




Custom Search