java 内部类和闭包

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 内部类和闭包相关的知识,希望对你有一定的参考价值。

内部类(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 内部类和闭包的主要内容,如果未能解决你的问题,请参考以下文章

闭包与内部类

java内部类和静态内部类

第三节:Java类和对象之代码块和内部类

Swift的闭包,枚举,类和结构体

java 内部类和外部类的关系

大数据必学Java基础(四十六):内部类和面向对象项目实战