A simple use of internal java class

package innerClass;
/**
 * Features
 * 1: enhanced encapsulation, by the internal class hidden inside the outer class, so that other classes can not be accessed outside the class.
 * 2: Enhanced maintainability.
 * 3: The inner class can access external members.
 * @author Administrator
 *
 */
class InnerClassDemo {
    String str="Out";
    static  String str1="static";
    public static void outstaticStr() {
        System.out.println();
    }
    public void outStr() {
        StaticInner staticInner=new StaticInner();
        staticInner.staticStr();
        String c= StaticInner.strInnerSt;
        String InnerC=Inner.InnerStaticelements;
        System.out.println(str);
        Inner inner =new Inner();
        System.out.println(inner.str);
        
        inner.innerStr ();
    }
    /**
     * Examples of internal class
     * @author Administrator
     * 1: example of the internal object class is an internal class, you treat it as an object. Can not be modified by static
     * 2: example of the internal reference to an external class Existence
     * 3: Examples of internal class members can directly access the outer class reference to access external class .this kind of external, internal static member class members directly with external access.
     * 4; external class to create an object of an inner class, then access members inside the class. Static inner class members directly. Members.
     * 5: Static member instance inner class must be fianl
     */
    public class Inner{
        String str="inner";
        static final String InnerStaticelements="staticInner";
        
        public  void innerStr()    {
            InnerClassDemo.outstaticStr();
            System.out.println(this.str);
            System.out.println(InnerClassDemo.this.str);
            
        }
    }
    /**
     * 
     * @author Administrator
     * Plus static inner class is static inner classes
     * Static inner classes do not have to open up a space in the heap to store a reference implementation of the outer class.
     * Static inner classes have direct access to static members outside of class (class requires no external external class members), access to non-static class members outside of class through new external class.
     * Static inner classes can create non-static members.
     * External class. The way members access to the internal static class members through a static inner classes through non-static member objects to access new static inner classes inside class
     */
     static class StaticInner{
        static String strInnerSt="rewre";
        String srt="staticInner";
        public void staticStr() {
            System.out.println(new InnerClassDemo().str);
            System.out.println(str1);
        }
    }
     /**
      * Local inner classes can not access private public settings, etc.
      * Partially inside the class definition in the method, the method also using
      * Local inner classes can not contain static variables
      * Access by external members, static external. The way members of the ordinary way is to use in the new exterior.
      * External class can not access the internal class members
      */
     public  void showLocalInner() {
          class  LocalInner{
             String  local="localInner";
              public  void localMethods(){
                 System.out.println(str);
             }
              
         }
          System.out.println(new LocalInner().local);
          localMethods ();
     }
     /**
      * Anonymous class is generally used in the development of the event, Andrews, GUI development, use only once.
      * Way can access non-static member outside the class members outside of class by way of new static member through the outer class. The access to external static member
      *
      */
     Runnable run =new Runnable() {
        
        @Override
        public void run() {
            // TODO Auto-generated method stub
            System.out.println(str);
            
        }
    };
    
}
package innerClass;

import innerClass.InnerClassDemo.Inner;

public class TestInnerClass {
    public static void main(String[] args) {
         Inner InnerClassDemo.Inner = new new InnerClassDemo (). New new Inner (); // Other types of access classes embodiment example of the internal 
         inner.innerStr ();
    
         System.out.println (InnerClassDemo.StaticInner.strInnerSt); // externally via an external static member class interior internal class members to access internal class.
         
    }
}

 

Guess you like

Origin www.cnblogs.com/shareAndStudy/p/12586861.html