Anonymous internal class, callback ...

xiaoxiao2021-03-06  99

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 STATIC

Void

Main (String [] args) throws exception {

New

A ();}} thread thread = new thread () {.......} This actually inherits the Thread class and generate an anonymous internal class; anonymous internal class can be created through the interface extension, It can be created by inheriting other classes; anonymous internal classes can achieve callbacks, callbacks are the basics of Java event driver mechanism ......... I have written like this in an article, I think people who answer questions Understand the problem, I am not asked. I think his answer is wrong. I don't know if my opinion is right. Please advise !!! As follows: Anonymous Inner Class (Anonymous Internal Class) Can EXTENDS (inherited) Other classes, can you IMPLEMENTS INTERFACE (interface)? Anonymous internal class is an internal class with no name. You cannot extends other classes, but an internal class can be implemented as an interface, and is implemented by another internal class. I think the answer should be: Yes, it can be. All can be.

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

New Post(0)