Java Object-oriented inner class of

What inner class?

It is seen from the beginning of the article, defined as inner classes: class definition of another class or method. According to different usage scenarios, inner classes can be divided into four categories: internal class members, local inner classes, anonymous inner classes and static inner classes. Each features and considerations are different, let them out.

Members of the inner class

As the name suggests, the internal members of the class is defined within a class, as a member of the class class. as follows:

public class Outer {
    
   public class Inner{
       
   }

}

Features are as follows:

  1. Members of the class may be internal authority modifier (EG. public,private等) The modified
  2. Members of the inner class can access all members of the outer class, (including private) a member of
  3. Members of the inner class is the default class contains a pointer to an external object reference
  4. As the use of thisthe same, when the member name or method name occur coverage, you can use the name of the outer class plus .this specify access outside the class members. Such as:Outer.this.name
  5. Members of the inner class can not define the staticmembers of the
  6. Members of the inner class created syntax:
Outer outer=new Outer();
Outer.Inner inner=outer.new Inner();

Partial inner class

Internal local class or a class defined in the method scope, the difference between it and the inner member classes differ only access.

public class Outer{
    public void test(){
        class Inner{
            
        }
    }
}

Features are as follows:

  1. Local inner classes can not have access modifiers
  2. Partial internal class can not be defined asstatic
  3. Partial internal class can not be defined staticmembers
  4. Partial internal default class comprises a reference to an external class object
  5. You can also use the local inner class Outer.thissyntax to access external development of class members
  6. Want to use the local variables inside the class or domain method, the variable must be finala

    After JDK1.8, no finalmodification, effectively finalcan be. What does that mean? It is not finalmodified, but if you add finalthe compiler can not complain.

Anonymous inner classes

No name anonymous inner classes are merged together and inheritance within the class

public class Outer{
    public List<String> list=new ArrayList<String>(){
        {
            add("test");
        }
    };
}

This is usually the most common syntax.
Features an anonymous inner classes are as follows:

  1. Anonymous inner classes of the blocks represent separate initialization block{}
  2. Anonymous inner classes want to use the method or variable domain, the variable must be finalmodified, after JDK1.8 effectively finalcan
  3. Anonymous inner classes included by default external object references
  4. Anonymous inner class represents a succession of dependent class

Static inner classes

Static inner classes with staticmodified internal member class

public class Outer {
    
   public static class Inner{
       
   }

}

Features are as follows:

  1. Static inner class is an internal class only four types does not contain a reference to the external object
  2. Static inner class defines staticmembers
  3. Nested classes can access any static data members and methods of the outer class.

Guess you like

Origin www.cnblogs.com/deityjian/p/11424420.html