Anonymous internal class in Java

Source: Internet
Author: User

Code is usually written by inheriting a class or implementing an interface. However, sometimes some code is only used once, so there is no need to write a specific subclass or implementation class. You can useAnonymous internal class. The most common scenarios are:ThreadApplications.

1. Do not use anonymous internal classes
① Inheritance
Abstract class Player
{
Public abstract void play ();
}

Public class FootBallPlayer extends Player
{
Public void play ()
{
System. out. println ("soccer ");
}
}

Public class AnonymousInnerClassTest
{
Public static void main (String [] args)
{
Player p1 = new FootBallPlayer ();
P1.play ();
}
}

② Interface
Interface IPlayer
{
Public void play ();
}

Public class IPlayFootballImpl implements IPlayer
{
Public void play ()
{
System. out. println ("soccer ");
}
}

Public class AnonymousInnerClassTest
{
Public static void main (String [] args)
{
IPlayer ip1 = new IPlayFootballImpl ();
Ip1.play ();
}
}


Ii. Use anonymous internal classes
① Inheritance
Abstract class Player
{
Public abstract void play ();
}


Public class AnonymousInnerClassTest
{
Public static void main (String [] args)
{
Player p2 = new Player (){
Public void play ()
{
System. out. println ("playing basketball ");
}
};
P2.play ();
}
}

② Interface
Interface IPlayer
{
Public void play ();
}

Public class AnonymousInnerClassTest
{
Public static void main (String [] args)
{
IPlayer ip2 = new IPlayer (){
Public void play ()
{
System. out. println ("playing basketball ");
}
};
}
}

3. Applications in threads
There are two ways to implement the Thread: ① inherit the Thread class ② implement the Runnable interface. An example of using anonymous classes is provided:

Public class ThreadTest
{
Public static void main (String [] args)
{
// Inherit the Thread class
Thread thread = new Thread (){
@ Override
Public void run ()
{
While (true)
{
Try
{
Thread. sleep (1000 );
System. out. println (Thread. currentThread (). getName ());
System. out. println (this. getName ());
}
Catch (InterruptedException e)
{
System. out. println (e. getMessage ());
}
}
}
};
Thread. start ();

// Implement the Runnable interface
Thread thread2 = new Thread (new Runnable (){
@ Override
Public void run ()
{
While (true)
{
Try
{
Thread. sleep (1000 );
System. out. println (Thread. currentThread (). getName ());
}
Catch (InterruptedException e)
{
System. out. println (e. getMessage ());
}
}
}
});
Thread2.start ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.