Beginners of Java - such as internal applications
Beginners of Java - such as internal applications
First, we look at what is the internal category?
Internal category (inner class) is another category in the definition of the class.
Then why do you need to use the internal category?
The main reason for the following three points:
(1) Internal category method can visit to the definition of the role of field data, including private data.
(2) within the same category can be included in the other categories hidden.
(3) When to define a callback function and do not want to prepare a large number code, the use of anonymous (anonymous) Internal relatively easy.
What is the correction? Callback (callback), the procedure is a common design pattern. In this mode, it can be pointed out that a specific time of the incident should take action. For example, pointed out that the mouse or select a menu item, what action should be taken.
In the category of internal study, we look at an example procedure in this section of the internal learning project, will focus on examples of this process of expansion.
In java.swing package, a Timer category, it can be used at a given time interval, issued a circular. For instance, if there is a clock in the process, then a second access may request a circular clock to update the screen.
Timer in the structure, the need to set a time interval, and inform the timer, when the inter-arrival time, what needs to be done operation.
How to inform the time to do anything? In many programming languages, can provide a function name, timers call it cyclical. However, in the Java standard class library category is based on object-oriented methods. It will transfer the object of a certain category to the timer, then, timers call this object methods. Because objects can carry additional information, send a target than a function to transfer more flexible.
Of course, call timers need to know which methods, and the object of transfer requests in the category achieved java.awt.event packets ActionListener interface. Below is the interface:
Public interface ActionListener
(
Void actionPerformed (ActionEvent event);
)
When the specified time interval, the call actionPerformer timer method. Assuming that every 10 seconds, print a message, "At the tone, the time is…", and then ring for the (beep), it should be a definition of the class to achieve ActionListener interface, and then will require the implementation of the statement on actionPerformed method in.
Class TimePrinter implements ActionListener
(
Public void actionPerformed (ActionEvent event)
(
Date now = new Date ();
System.out.println ( "At the tone, the time is" + now);
Toolkit.getDefaltToolkit (). Beep ();
)
)
Need to pay attention to the ActionEvent actionPerformed method parameters. This parameter provides a time-related information. For example, a target the source of this incident.
Next, the structure of this type of an object and pass it to the Timer constructor.
ActionListener listener = new TimePrinter ();
Timer t = new Timer (10000, listener);
Timer structure parameter is the first time issued a circular interval, and his unit is milliseconds. The second parameter is sniffers object.
Finally, the timer started:
T.start ();
After starting timer, procedures dialog box will pop up a message, and wait for the user to click on the OK button to terminate the program execution. In the operating procedures for users at the same time, every 10 seconds a show that the current time.
Need Note: In addition to this procedure, and import javax.swing .* java.util .*, but also by class name into the javax.swing.Timer. This eliminates the javax.swing.Timer java.util.Timer and between the two of justice. Java.util.Timer here is a case of this has nothing to do with the class, which mainly used for scheduling background tasks.
All procedures code as follows:
Import java.util .*;
Import java.awt .*;
Import java.awt.event .*;
Import javax.swing .*;
Import javax.swing.Timer;
Public class TimerTest
(
Public static void main (String [] args)
(
ActionListener listener = new TimerPrinter ();
Timer t = new Timer (10000, listener);
T.start ();
JOptionPane.showMessageDialog (null, "Quit program?");
System.exit (0);
)
)
Class TimerPrinter implements ActionListener
(
Public void actionPerformed (ActionEvent event)
(
Date now = new Date ();
System.out.println ( "At the tone, the time is" + now);
Toolkit.getDefaultToolkit (). Beep ();
)
)
First, the use of internal targets state visit
The syntax of the internal complexity. In view of this, but we have chosen not apply a simple example illustrates the use of internal way. Below nearly an analysis TimerTest example, a TalkingClock and abstract category. Construction of a need to provide voice clock when two parameters: the intervals and issued a circular ring tones switch signs.
Class TalkingClock
(
Public TalkingClock (int interval, boolean beep)
(
…..;
)
Public void start ()
(
……;
)
Private int interval;
Private boolean beep;
Private class TimerPrinter implements ActionListener
(
…..;
);
)
Need to pay attention to, here in TalkingClock TimerPrinter category within category. This does not mean that everyone has a TimerPrinter TalkingClock examples domain. As indicated earlier, TimePrinter TalkingClock the object is constructed.
TimerPrinter category in a private house category (private inner class). This is a security mechanism. In this way, the only category TalkingClock method can generate TimePrinter object.
Only within such categories can be private, and only with conventional packet type of visibility, or a total of visibility.
Below are the details of the TimePrinter category. Need to note that actionPerformed method checks issued before the beep ringtones signs.
Private class TimerPrinter implements ActionListener
(
Public void actionPerformed (ActionEvent event)
(
Date now = new Date ();
System.out.println ( "At the tone, the time is" + now);
If (beep)
Toolkit.getDefaultToolkit (). Beep ();
)
);
In order to run this program, the internal targets have a kind of implicit use, it points to build its external object type. Cited in this category within the definition of is not visible. However, in order to illustrate the concept, we will invoke external object type called outer. So actionPerformed method will be equivalent to the following forms:
Private class TimerPrinter implements ActionListener
(
Public void actionPerformed (ActionEvent event)
(
Date now = new Date ();
System.out.println ( "At the tone, the time is" + now);
If (outer.beep)
Toolkit.getDefaultToolkit (). Beep ();
)
);
Below is a test of the completion of internal procedures:
Import java.awt .*;
Import java.awt.event .*;
Import java.util .*;
Import javax.swing .*;
Import javax.swing.Timer;
Public class InnerClassTest
(
Public static void main (String [] args)
(
TalkingClock clock = new TalkingClock (1000, true);
Clock.start ();
JOptionPane.showMessageDialog (null, "Quit program?");
System.exit (0);
)
);
Class TalkingClock
(
Public TalkingClock (int interval, boolean beep)
(
This.interval = interval;
This.beep = beep;
)
Public void start ()
(
ActionListener listener = new TimerPrinter ();
Timer t = new Timer (interval, the listener);
T.start ();
)
Private int interval;
Private boolean beep;
Private class TimerPrinter implements ActionListener
(
Public void actionPerformed (ActionEvent event)
(
Date now = new Date ();
System.out.println ( "At the tone, the time is" + now);
If (beep)
Toolkit.getDefaultToolkit (). Beep ();
)
);
)
Posted on 2007-04-16 17:04 skycc reading (134) Comments (0) edit collections cited






