码迷,mamicode.com
首页 > 编程语言 > 详细

java 内部类和闭包

时间:2017-11-18 21:00:51      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:回调   count()   creat   this   update   ted   它的   time   except   

内部类(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());
    }
}

局部内部类

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 changeOuterInfo(){
                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());
    }
}

静态内部类

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 changeOuterInfo(){
                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 are "+Outer.count+" Outer instances have 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());
    }
}

匿名内部类

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 changeOuterInfo(){
                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 are "+Outer.count+" Outer instances have 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)和闭包(closure)

函数式编程中常见的概念,内部作用域中调用定义它的外部作用域中的成员或者方法。示例,在局部内部类Inner2的changeOuterInfo()方法中调用外部Outer类的showTime()方法,注意调用方式(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 changeOuterInfo(){
                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 are "+Outer.count+" Outer instances have 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 内部类和闭包

标签:回调   count()   creat   this   update   ted   它的   time   except   

原文地址:http://www.cnblogs.com/wangxiaohei/p/7857732.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!