Java Reference
In-Depth Information
Purpose
To define an inner class whose scope is restricted to a single method or the
methods of a single class
When you compile the source files for a program that uses inner classes, have a look
at the class files in your program directoryȌyou will find that the inner classes are
stored in files with curious names, such as
DataSetTester3$1$RectangleMeasurer.class . The exact names aren't
important. The point is that the compiler turns an inner class into a regular class file.
ch09/measure3/DataSetTester3.java
1 import java.awt.Rectangle;
2
3 /**
4 This program demonstrates the use of an inner class.
5 */
6 public class DataSetTester3
7 {
8 public static void main(String[] args)
9 {
10 class RectangleMeasurer implements
Measurer
11 {
12 public double measure(Object anObject)
13 {
14 Rectangle aRectangle = (Rectangle)
anObject;
15 double area
16 = aRectangle.getWidth() *
aRectangle.getHeight();
17 return area;
18 }
19 }
20
21 Measurer m = new RectangleMeasurer();
22
23 DataSet data = new DataSet(m);
24
25 data.add( new Rectangle( 5 , 10 , 20 , 30 ));
26 data.add( new Rectangle( 10 , 20 , 30 , 40 ));
404
405
Search WWH ::




Custom Search