Use of inner classes and anonymous inner classes in Java

Source: Internet
Author: User

Part of the content reference http://www.imooc.com/
Inner class:

What is the inner class in Java
Q: What is an internal class?
A: The Inner class (Inner Class) is the class defined in another class. Corresponding to this, the class containing the inner class is called the outer class.
Q: Why do you define a class in another class? Clear refreshing cool independent of a kind of how good ah!!
A: The main functions of the inner class are as follows:
1. The inner class provides a better encapsulation that hides the inner class within the outer class and does not allow other classes in the same package to access the class
2. The method of the inner class can access all the data of the external class directly, including the private data
3. Functions implemented by internal classes can be implemented using external classes, but sometimes it is easier to use inner classes, overriding methods of external classes
Q: How many internal classes are there?
A: The inner class can be divided into the following types:
1. member Inner class
2. Static Inner class
3. Method Inner Class
4. Anonymous inner class

Here is an example of an internal class

//External class HelloWorld Public classHelloWorld {//Inner class inner, class inner inside class HelloWorld     Public classInner {//method of inner class         Public void Show() {System. out. println ("Welcome to imooc!"); }    } Public Static void Main(string[] args) {//Create an external class objectHelloWorld Hello =NewHelloWorld ();//Create inner class objectInner i = hello.NewInner ();//method to invoke the Inner class objectI.show (); }}

anonymous inner class:
is a class that cannot have a name, so there is no way to reference them. They must be declared as part of the new statement at the time of creation. This will take another form of the new statement, as follows: New < class or interface > < class body >
Functions are:
1. As with internal classes, you can extend, override, or add new methods to external classes.
2. For implementing interfaces
3. No class name

//Create a new interface Public  interface Animal {    voidEat ();voidSleep ();}//Create a new class that uses inner classes and internal anonymous classes to implement interfaces within a class Public  class Zoo {    Private  class Tiger implements Animal {         Public void Eat() {System.out.println ("Tiger Eat"); } Public void Sleep() {System.out.println ("Tiger Sleep"); }    }/** * Here do not understand, animal is not an interface, how can you define the method?     *---Answer interface can be a return type, specifying that the returned type must be the class that implements the current interface. * @return * *    /** *//Because the interface is not instantiated, it must be implemented through a class, where it is used to return a (object that implements the interface) * There are two specific ways of writing, 2 easy to understand * @return * *    //---------------------------1-----Anonymous inner class--------------------------------Animal GetAnimal1 () {return NewAnimal () { Public void Eat() {System.out.println ("---1---animal eat"); } Public void Sleep() {System.out.println ("---1---animal sleep");    }        }; }//---------------------------2------inner class--------------------------Animal GetAnimal2 () {return NewTempanimal (); } class Tempanimal implements animal{intA =0; Public void Eat() {System.out.println ("---2---animal eat"); } Public void Sleep() {System.out.println ("---2---animal sleep"); }       }}//Create a new test class Public  class Test {     Public Static void Main(string[] args) {Zoo z =NewZoo ();        Animal an = Z.getanimal1 ();        An.eat ();        An.sleep ();        Animal one = Z.getanimal2 ();        One.eat ();    One.sleep (); }}
Anonymous inner classes are a simplified way of writing internal classes:return NewWrapper{...}; Equivalent to: Wrapped extends wrapper{...}return NewWrapped (); Is the anonymous inner class The only thing that works? Think about this. Case: interface ICount {    int Count();} class Parent {    inti =0;int Count() {returni++; }} has a class child that wants to inherit the parent'sCount() method, and to implement the Icount interface.Countmethod, what to do at this time? The inner class is ready to go: class  child extends Parent {ICount GetCount () {return NewICount {inti =0;int Count() {return(I *=2); }    }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Use of inner classes and anonymous inner classes in Java

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.