Java notes 18. Inner Classes and anonymous classes

Source: Internet
Author: User

inner classes and anonymous classes reprint please indicate source:http://blog.csdn.net/u012637501( embedded _ small J Sky )
first, the internal class 1. DefinitionA class is defined inside a class, which is a nested class (inner Class). There are several features: (1) A nested class can directly access the members (variables and methods) of the class that nested it, including private members. However, the members of a nested class cannot be accessed directly by the classes that nest it. (2) The inner class makes the program code more compact and the program more modular. 2. Inner class implementation Method (1) defines and uses an inner class in a classThe scope of use of nested classes defined directly in a class is limited to the inner of this class . There is no difference between the definition of an inner class and the definition of a normal class, and an inner class can directly access and reference all the variables and methods of its outer class, just like the other non-static members in an outer class, and unlike an outer class, an inner class can be declared as private or protected . A. Inner classes are referenced by external classesSource: Innerclassdemo.java
External class outer{  String name= "I am an external class member variable";  void Outermethod ()//define a member method in the outer class, implement create an inner class instance and call its member method  {   inner in=new inner ();   In.dispaly ();  }  Inner class  inner  {   void display ()   {    System.out.println ("Method of inner class print ' name ' = "+name);}}}  /*-------------------------------------------*/public class Innerclassdemo {public static void main (string[] args) { C17/>outer Outer = new Outer ();  Outer.outermethod (); }}

Printing information:
Analysis: The source implements an inner class inner in the outer class outer, and defines a member method in the inner classes to use the member name of the outer of the outer class. In addition, an inner class inner object can only be instantiated in the Outer class outer, and it is not allowed to instantiate the inner class outside the outer class unless the inner is declaredpublic (described later). from the point of view of memory distribution, in fact, an internal class object holds a reference to an external partial object (outer.this), when a variable (method) is accessed in the member method of the inner class. If the variable is not defined in both the method and the inner class , the invocation is passed to the reference of which external class object is saved in the inner class . Call this variable (method) from the reference of which external class object.
Note: Only if the variable does not exist, it is passed to the outer.this. variable. B. Internal classes are referenced by classes outside the outer classan inner class can be called from outside the class by creating an object.(1) Declare the inner class as public;(2) in a class other than an outer class, create an instance of an external class and then create an instance of the inner class through that instanceform:Outer Outer = new Outer ();Outer.Inner inner=outer.new Inner ();Source: Innerclassdemo
External class outer{private int number=100; public class Inner {  private int number=200;  void print (int number)  {   System.out.println ("I am innerclass" + "\ n" + "parameter number=" +number+ "\ n" + "inner class member         number= "+this.number+" \ n "+" external class member number= "+outer.this.number);  }}//outside the public class Innerclassdemo {public static void main (string[] String) {  Outer Outer = new Outer ();//Instantiate an O Uter object  outer.inner inner=outer.new Inner ();//Instantiate a Inner object  inner.print (300);//Call the member method of the External class}}

Printing information:
Analysis: Observe the above program, you will find three number variables, the difference is:Number : Reference is the formal parameter of the print function;This.number: Refers to a member variable in the inner class;Outer.this.number: A member variable in the referenced external class Outer. 2. Internal classes defined in the methodnested classes are not intelligently defined in classes, but they can also be defined within the scope of several blocks. Source: Innerdemo.java
External class outer{Private String str= "Yao"; public void Outerprint () {    int i=10;    Inner class   Inner   {    void dispaly (final int number)    {          System.out.println (str);     SYSTEM.OUT.PRINTLN (number);    System.out.println (i);//The statement is wrong, the inner class in the method cannot access the non-final variable of the method    }   }   //Instantiate the inner class and call its member method   Inner Inner = New Inner ();   Inner.dispaly (11); }}public class innerdemo{public static void Main (string[] args) {  Outer Outer = new Outer ();  Outer.outerprint (); }}

Printing information:
Analysis: A local variable of the final type in the inner class intelligent access method defined in the method rollup (cannot access I), because the local variable defined by final is equivalent to a constant, and its declaration period investigates the life cycle of the method's operation.
second, anonymous internal class 1. Anonymous classbefore learning the anonymous inner class, let's take a look at an example of implementing an inner class that inherits from another class, which is described as defining an inner class Inner,inner class in class outer and inheriting Class A. Source: Outer.java
Classes Aabstract class a{abstract void Display ();} The Outer class, in its main method, defines an inner class that inherits from the class Apublic class Outer {//main member method public static void main (string[] args) {   //inner class 
   class Inner extends A   {    void display () {     System.out.println ("I am Inner class.");    }   }   New Outer (). Callinner (New Inner ());//Create a Outer instance and call its member method, the method parameter is an inner class object}//callinner member method public void Callinner (a a) {  A.display (); }}
Printing information:
Analysis: By new Outer (). Callinner (New Inner ()); The Outer.callinner method is called in the Outer.main method, and the parameter requirement passed to the Callinner method is a subclass object that implements the method in Class A. So, we define an inner class inner that inherits from Class A in the Outer.main method, which is used only once in this inner class. It feels too complicated for the program above, so what can we do to abbreviate it? Of course. In fact, when we call the Callinner method, we do not need to implement the definition class's inner, you can temporarily create an instance object of the anonymous subclass of Class A when passing parameters to the Callinner method, the code is as follows:Source: Outer.java
Classes Aabstract class a{abstract void display ();//declares an abstract class}//external classes public class Outer {//main member method public static void main (Strin G[] (args) {  new Outer (). Callinner (New A () {   void display () {    System.out.println ("I am childclass of a,but i hav E not name-' Inner ');   }  }); }//callinner member method public void Callinner (a a) {  a.display ();}}
Printing information:
Analysis: By the results of the program printing, we can see that the Outer.main method calls the Outer.callinner method, the parameter passed to the Callinner method is a subclass object that implements the method in Class A. In the above program it is equivalent to defining a subclass of Class A, but this subclass has no class name, and then creates an instance object of the subclass, such that the class is anonymous class. 2. Forms of Anonymous classesif we implement a Class A, then an instance object of its anonymous subclass can be represented as such. form:new A () {} function: Produces an instance object of an anonymous subclass of Class A , and all implementation code for the anonymous subclass (including member methods and member variables) is incremented between the braces.

Java notes 18. Inner Classes and anonymous classes

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.