Internal class, anonymous internal class, callback!

xiaoxiao2021-03-06  111

Basic theory:

-------------------------------------------------- ---

About Java Internal Class: Definition of an internal class is a class defined within another class.

The reason for existence is:

1. An internal class object can access the implementation of the object that creates it, including private data. That is, the internal class instance is privileged for which one class of it is.

2. For other classes in the same package, the internal class can hide, in other words, the internal class regardless of the visibility of the method, even the public, in addition to the inclusive class, other classes cannot be used.

3. Anonymous internal classes can be very convenient to define callbacks.

4. Use the internal class to write event drivers very convenient.

In fact, it is true to define the callback - further is the event driver.

Interfaces and callbacks: Programming a common mode is a callback mode, in which mode you can specify a method of callback object when a specific time occurs. -------------------------------------------------- Note: THIS in anonymous classes and internal classes: Sometimes we use some internal classes and anonymous classes. When this this this this this this this is anonymous or the internal class itself when using this in anonymous class. At this time, if we want to use the external classes and variables, you should add the class name of the external class. As described below:

Public class a {

int

i

=

1

Public a () {thread thread

=

New

Thread () {public

Void

Run () {

for

(;;) {A.

THIS

. run ();

Try

{SLEEP

1000

}

Catch

(InterruptedException IE) {}}}}; thread.start ();} public

Void

Run () {system.out.println

"

i =

"

i); i

PUBLIC StaticVoid

Main (String [] args) throws exception {

New

A ();}}

In this example, Thread is an anonymous object, in its definition, its RUN function is used in the RUN function. At this time, because the function is the same name, it will not be called directly. There are two ways at this time that one is to change the external Run function, but this approach is not advisable for a development to the middle. Then use this example in this example to add this reference to the THIS reference to the external class. -------------------------------------------------- For this example:

THIS

.test

New

Inner () {public

Void

Method1 () {system.out.print

"

1111

"

PUBLIC

Void

Method2 () {system.out.print

"

22222

"

);}});

At this time, call the test () method, when is the INNER class Method1 and Method2 call? Is it also the THIS object to send a message (such as incoming a parameter)? Or is it directly explicit call? ? For the Inner class, in addition to this class, this.test (... THIS in that sentence, it can call the Inner class method, elsewhere, however, this also requires you to save you in the class The reference to this internal class instance can be. One more, the internal class is to exist in the way to call the outside at a time - this is the callback.

To illustrate the method of internal class instances, you can only call in an instance of the inclusive class, and other places cannot be called, give an example below:

Java2 practical tutorial source:

/ *

* An application to demonstrate the use of internal classes

* /

/ *

* Class Outer

* /

Class Outer {Private Static STATIC

int

Size;

/ *

* Internal INNER's statement

* /

Public class inner {private

int

Size;

/ *

* Method DOSTUFF ()

* /

public

Void

Dostuff

int

Size) {SIZE

;

//

Access local variable

THIS

.size

;

//

Access the member variables of its internal classes

Outer.

THIS

.size

;

//

Access the member variables of its external class

System.out.println (SIZE

"

"

THIS

.size

"

"

Outer.

THIS

.size);}}

//

Internal class Inner end

/ *

* Example Method for Class Outer Testinner () Method

* /

public

Void

Testinner () {Inner I

=

New

INNER (); I. DOSTUFF

5

}

/ *

* Main () method

* /

Public Static

Void

Main (String [] a) {Outer O

=

New

Outer (); o.testinner ();}}

//

End of class OUTER

------------------------------------------------ then, How to use internal classes, anonymous classes implement event processing?

Java --- Event Adapter

1. Event adapter - EventAdapter

The mouse adapter is used in the following example:

Import java.aw.

*

Import java.awt.event.

*

PUBLIC CLASS MouseclickHandler Extends Mouseadaper {public

Void

MouseClicked (MouseEvent E)

//

Only realize the needs

{...}}

The event adapter classes defined in the java.awt.event package include the following: 1. ComponentAdapter 2. ContaineraDapter 3. FocusAdapter 4. KeyAdapter 5. MouseAdapter 6. MouseMotionAdapter 7. WindowAdapter (window adapter)

2. Implement event processing with internal class

Inner class is a class defined in another class, using the main reason for the internal class is due to: ◇ An internal class object can access the members of the external class and variables, including private members. ◇ When implementing an event listener, an internal class is used, and anonymous class programming is very easy to implement its function. ◇ Write an event driver, which is very convenient. Therefore, the place where the internal class can be applied is often in an event processing mechanism of AWT.

Example 5.11

Import java.aw.

*

Import java.awt.event.

*

Public class innerclass {private frame f; private textfield tf; public innerclass () {f

=

New

Frame

"

Inner Classes EXAMPLE

"

); TF

=

New

Textfield

30

); Public voidi launchframe () {label label

=

New

Label

"

Click and Drag The Mouse

"

); F.Add (label, borderlayout.north); f.add (tf, borderlayout.south); f.addmouseMotionListener

New

MyMouseMotionListener ());

/ *

Parameter is an internal class object

* /

F.setsize

300

,

200

); F.setvisible

True

} Class mymousemotionlistener extends mousemotionadapter {

/ *

Internal class begins

* /

public

Void

MouseDragged (MouseEvent E) {String S

=

"

Mouse Dragging: x =

"

E.GETX ()

"

Y =

"

E.GETY (); tf.settext (s);}}; public static

Void

Main (String Args []) {InnerClass Obj

=

New

InnerClass (); obj.launchframe ();}}

//

Internal class end

}

3. Anonymous class

When an internal class is just used when you create such an object, and the new categories you want to generate inherit in an existing parent class or implement an interface, you can think about anonymous class, because the anonymous class is not named. Therefore, it does not have a construction method, which needs to display a constructor of a non-arranging parent class, and rewrite the method of the parent class. The so-called anonymity is that the type of even name is not, but only the constructor of the parent class is called.

Example 5.12

Import java.aw.

*

Import java.awt.event.

*

Public class anonymousclass {private frame f; private textfield tf; public anonymousclass () {f

=

New

Frame

"

Inner Classes EXAMPLE

"

); TF

=

New

Textfield

30

PUBLIC

Void

Launchframe () {label label

=

New

Label

"

Click and Drag The Mouse

"

); F.Add (label, borderlayout.north); f.add (tf, borderlayout.south); f.addmouseMotionListener

New

MouseMotionAdapter () {

//

Anonymous class

public

Void

MouseDragged (MouseEvent E) {String S

=

"

Mouse Dragging: x =

"

E.GETX ()

"

Y =

"

E.GETY (); tf.settext (s);}});

//

End of anonymous class

F.setsize

300

,

200

); F.setvisible

True

PUBLIC STATIC

Void

Main (String Args []) {anonymousclass obj

=

New

AnonymousClass (); obj.launchframe ();}}

In fact, careful analysis, Example 5.11 and 5.12 are all exactly the same function, but it is only different. 5.11 The event handling class is an internal class, and 5.12's event handling class is anonymous class, which can be said to be increasingly unclear from the relationship between the class, but the program is increasingly concise. Familiar with these two ways also help to write a graphical interface.

I personally practice it, I will understand more deeply. However, in the .NET, event mode is realized, there is a simpler, such a lot of articles, but may also continue to discuss discussions.

转载请注明原文地址:https://www.9cbs.com/read-103275.html

New Post(0)