Java Reference
In-Depth Information
59
60
public void
enlarge() {
61 circle.setRadius(circle.getRadius() +
2
);
62 }
63
64
public void
shrink() {
65 circle.setRadius(circle.getRadius() >
2
?
66 circle.getRadius() -
2
: circle.getRadius());
67 }
68 }
enlarge method
As an exercise, add the code for handling the
Shrink
button to display a smaller circle when
the
Shrink
button is clicked.
the
Shrink
button
15.3
✓
✓
Why must a handler be an instance of an appropriate handler interface?
Check
15.4
Explain how to register a handler object and how to implement a handler interface.
Point
15.5
What is the handler method for the
EventHandler<ActionEvent>
interface?
15.6
What is the registration method for a button to register an
ActionEvent
handler?
An inner class, or nested class, is a class defined within the scope of another class.
Inner classes are useful for defining handler classes.
Key
Point
Inner classes are used in the preceding section. This section introduces inner classes in detail.
First, let us see the code in Figure 15.7. The code in Figure 15.7a defines two separate classes,
Test
and
A
. The code in Figure 15.7b defines
A
as an inner class in
Test
.
public class
Test {
...
}
// OuterClass.java: inner class demo
public class
OuterClass {
private int
data;
public class
A {
...
}
/** A method in the outer class */
public void
m() {
// Do something
}
(a)
// An inner class
class
InnerClass {
/** A method in the inner class */
public void
mi() {
// Directly reference data and method
// defined in its outer class
data++;
m();
public class
Test {
...
// Inner class
public class
A {
...
}
}
}
}
}
(b)
(c)
F
IGURE
15.7
Inner classes combine dependent classes into the primary class.
The class
InnerClass
defined inside
OuterClass
in Figure 15.7c is another example
of an inner class. An inner class may be used just like a regular class. Normally, you define
Search WWH ::
Custom Search