Java internal classes and closures

Source: Internet
Author: User

Internal classes (inner class)
public class Outer{    private String name;    class Inner{        public String getOuterName(){            return Outer.this.name;        }        public void setOuterName(String name){            Outer.this.name = name;        }    }    public static void main(String[] args){        Outer outer = new Outer();        Outer.Inner inner = outer.new Inner();        inner.setOuterName("wangtengfei");        System.out.println(inner.getOuterName());    }}
Local inner class
public class outer{private String name;    Private String sexual = "male";        Class inner1{public String getoutername () {return Outer.this.name;        } public void Setoutername (String name) {Outer.this.name = name; }} public void UpdateName (final String name,final string sexual) {class inner2{public void Chan                Geouterinfo () {Outer.this.name = name;            Outer.this.sexual = sexual;        }} Inner2 Inner2 = new Inner2 ();    Inner2.changeouterinfo ();    } public String getsexual () {return sexual;        } public static void Main (string[] args) {Outer Outer = new Outer ();        Outer.inner1 inner = outer.new Inner1 ();        Inner.setoutername ("Wangtengfei");        System.out.println ("Name:" +inner.getoutername () + ", Sexual:" +outer.getsexual ());        Outer.updatename ("Iris", "female"); System.out.println ("Name:" +inner.getoutername () + ", Sexual:" +oUter.getsexual ()); }}
Static Inner class
public class outer{private String name;    Private String sexual = "male";    public static int count;    Public Outer () {count++;        } class inner1{public String getoutername () {return Outer.this.name;        } public void Setoutername (String name) {Outer.this.name = name; }} public void UpdateName (final String name,final string sexual) {class inner2{public void Chan                Geouterinfo () {Outer.this.name = name;            Outer.this.sexual = sexual;        }} Inner2 Inner2 = new Inner2 ();    Inner2.changeouterinfo ();    } public String getsexual () {return sexual; public static class Inner3{public String Getouterinstancecount () {return "there is" +outer.count+        "Outer instances has been created";        }} public static void Main (string[] args) {Outer Outer = new Outer (); Outer.inner1 inner = outer.new Inner1 ();        Inner.setoutername ("Wangtengfei");        System.out.println ("Name:" +inner.getoutername () + ", Sexual:" +outer.getsexual ());        Outer.updatename ("Iris", "female");        System.out.println ("Name:" +inner.getoutername () + ", Sexual:" +outer.getsexual ());        Inner3 Inner3 = new Inner3 ();    System.out.println (Inner3.getouterinstancecount ()); }}
Anonymous inner class
public class outer{private String name;    Private String sexual = "male";    public static int count;    Public Outer () {count++;        } class inner1{public String getoutername () {return Outer.this.name;        } public void Setoutername (String name) {Outer.this.name = name; }} public void UpdateName (final String name,final string sexual) {class inner2{public void Chan                Geouterinfo () {Outer.this.name = name;            Outer.this.sexual = sexual;        }} Inner2 Inner2 = new Inner2 ();    Inner2.changeouterinfo ();    } public String getsexual () {return sexual; public static class Inner3{public String Getouterinstancecount () {return "there is" +outer.count+        "Outer instances has been created";                }} public void ShowTime () {Thread t = new Thread (new Runnable () {public void run () { Int Time = 60;                int i = 0;                    while (i< time) {System.out.println (Thread.CurrentThread (). toString ());                    System.out.println ((New Date (System.currenttimemillis ())). ToString ());                    i+=5;                    try {thread.sleep (5*1000);                    } catch (Interruptedexception e) {e.printstacktrace ();        }                }            }        });        Executorservice Executorservice = Executors.newcachedthreadpool ();        Executorservice.execute (t);    Executorservice.execute (t);        } public static void Main (string[] args) {Outer Outer = new Outer ();        Outer.inner1 inner = outer.new Inner1 ();        Inner.setoutername ("Wangtengfei");        System.out.println ("Name:" +inner.getoutername () + ", Sexual:" +outer.getsexual ());        Outer.updatename ("Iris", "female"); System.out.println ("Name:" +inner.getoutername () + ", Sexual:"+outer.getsexual ());        Inner3 Inner3 = new Inner3 ();        System.out.println (Inner3.getouterinstancecount ());        Outer.showtime ();    System.out.println ("main process end!"); }}
Callbacks (callbacks) and closures (closure)

A common concept in functional programming, where a member or method in the outer scope that defines it is called inside the scope. example, calling the ShowTime () method of the external Outer class in the Changeouterinfo () method of the local inner class Inner2 (), notice the Invocation method (Outer.this.showTime ())

public class outer{private String name;    Private String sexual = "male";    public static int count;    Public Outer () {count++;        } class inner1{public String getoutername () {return Outer.this.name;        } public void Setoutername (String name) {Outer.this.name = name; }} public void UpdateName (final String name,final string sexual) {class inner2{public void Chan                Geouterinfo () {Outer.this.name = name;                Outer.this.sexual = sexual;            Outer.this.showTime ();        }} Inner2 Inner2 = new Inner2 ();    Inner2.changeouterinfo ();    } public String getsexual () {return sexual; public static class Inner3{public String Getouterinstancecount () {return "there is" +outer.count+        "Outer instances has been created";            }} public void ShowTime () {Thread t = new Thread (new Runnable () {public void Run () {int time = 60;                int i = 0;                    while (i< time) {System.out.println (Thread.CurrentThread (). toString ());                    System.out.println ((New Date (System.currenttimemillis ())). ToString ());                    i+=5;                    try {thread.sleep (5*1000);                    } catch (Interruptedexception e) {e.printstacktrace ();        }                }            }        });        Executorservice Executorservice = Executors.newcachedthreadpool ();        Executorservice.execute (t);    Executorservice.execute (t);        } public static void Main (string[] args) {Outer Outer = new Outer ();        Outer.inner1 inner = outer.new Inner1 ();        Inner.setoutername ("Wangtengfei");        System.out.println ("Name:" +inner.getoutername () + ", Sexual:" +outer.getsexual ());        Outer.updatename ("Iris", "female"); System.out.println ("Name: "+inner.getoutername () +", Sexual: "+outer.getsexual ());        Inner3 Inner3 = new Inner3 ();        System.out.println (Inner3.getouterinstancecount ());//Outer.showtime ();    System.out.println ("main process end!"); }}

Java internal classes and closures

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.