Java anonymous internal class

Source: Internet
Author: User

An anonymous java internal class is generally an object without a variable pointing to. It can only be used once.


The following code is very troublesome to inherit and rewrite the eat method to use the eat method of the abstract class People. This is also true for interfaces.

Package com. lubby. nosynchronizedtest; public abstract class People {public abstract void eat ();}
Package com. lubby. nosynchronizedtest; public class Teacher extends People {@ Overridepublic void eat () {System. out. println ("the instructor is eating");} public static void main (String [] args) {People people = new Teacher (); people. eat ();}}

Check the following code. Between new
Package com. lubby. nosynchronizedtest; public class Teacher {public static void main (String [] args) {new People () {@ Overridepublic void eat () {System. out. println ("I'm eating ");}}. eat ();}}

Take a closer look and find out if this method is frequently used by threads. Generally, thread creation inherits the Threa abstract class or Runable interface.


Package com. lubby. nosynchronizedtest; public class Teacher {public static void main (String [] args) {Thread thread1 = new Thread () {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();}}

















Related Article

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.