Java Anonymous inner class

Source: Internet
Author: User

Java anonymous inner classes usually have no object variable points. It can only be used once


For example, the following code, to use the abstract class people eat also inherits and replicates the method eat way, very cumbersome. The same situation applies to interfaces as well.

Package com.lubby.nosynchronizedtest; Public  Abstract class people {public abstract void Eat ();} <pre name= "code" class= "Java" >package Com.lubby.nosynchronizedtest;public class Teacher extends people {@ overridepublic void Eat () {System.out.println ("Teacher is Eating");} public static void Main (string[] args) {people people = new Teacher ();p eople.eat ();}}

Look at the following code for example. is not very easy. Between New one
Package Com.lubby.nosynchronizedtest;public class Teacher {public static void main (string[] args) {New people () {@Overrid epublic void Eat () {System.out.println ("I'm Eating");}. Eat ();}}

Look carefully. Discover whether threads are often used in such a way. Thread creation is generally inherited Threa abstract class or Runable interface


Package Com.lubby.nosynchronizedtest;public class Teacher {public static void main (string[] args) {Thread thread1 = new Th Read () {public void run () {System.out.println ("Thread 1 is Running");}}; Thread1.start ();}}


Package Com.lubby.nosynchronizedtest;public class Teacher {public static void main (string[] args) {Runnable runable = new Runnable () {@Overridepublic void run () {System.out.println ("Run ...");}; Thread thread = new Thread (runable); Thread.Start ();}}

















Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

Java Anonymous inner class

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.