"Java" "Anonymous inner class"

Source: Internet
Author: User


/*
Anonymous inner class
is the simplified notation of the inner class.

Premise: There is a class or interface
The class here can be either a concrete class or an abstract class.

Format:
New class name or interface name () {
Override method;
}

What is the essence of it?
is an anonymous object that inherits the class or implements the interface's subclass.
*/

Interface Study
Interface Study {
public abstract void Read ();

public abstract void Write ();
}

Abstract class Play
Abstract class Play {
public abstract void Kongfu ();
}

Hulk Implementation Interface Study
Class Hulk implements Study {
public void Read () {
System.out.println ("I can read");
}

public void Write () {
System.out.println ("I can write");
}

}

Pander inheriting abstract class play
Class Pander extends play{
public void Kongfu () {
System.out.println ("I love Kongfu");
}
}

public class Anonymousinnerclass {
public static void Main (string[] args) {

Method One: Do not use anonymous inner classes
If you use it only once, creating an inherited class and implementing a class is a waste of memory, so the anonymous inner class applies only once.
Study s = new Hulk ();
S.read ();
S.write ();

Play p = new pander ();
P.kongfu ();
System.out.println ("------------\ n");

Method Two: Use anonymous inner classes
New Study () {
public void Read () {
System.out.println ("I can read");
}

public void Write () {
System.out.println ("I can write");
}
}.read ();

New Study () {
public void Read () {
System.out.println ("I can read");
}

public void Write () {
System.out.println ("I can write");
}
}.write ();

New Play () {
public void Kongfu () {
System.out.println ("I love Kongfu");
}
}.kongfu ();

System.out.println ("------------\ n");


Method Three: Another way to simplify the anonymous inner class

Study ss= New Study () {
public void Read () {
System.out.println ("I can read");
}

public void Write () {
System.out.println ("I can write");
}
};

Ss.read ();
Ss.write ();

Play pp =new play () {
public void Kongfu () {
System.out.println ("I love Kongfu");
}
};
Pp.kongfu ();

}

}

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

"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.