美文网首页
Kotlin学习-1.object用法

Kotlin学习-1.object用法

作者: CODERLIHAO | 来源:发表于2018-09-29 13:31 被阅读0次

    1、使用object声明类似java中的静态内部类

    interface Moveable{
          fun move(){
          }
    }
    
    class Outer{
             private var a:String?=null
              //被编译成静态内部类
              object c:Moveable{
                      override fun move() {
                           //接口的默认实现
                            super.move()
                            //静态内部类无法访问外部内非静态变量
                            //print(a)
                        }
                }
    }
    
    编译后生成的class文件

    使用 jad 将class文件转java
    jad -s .java Outer.class

    public final class Outer
    {
        public static final class c
            implements Moveable
        {
    
            public void move()
            {
                Moveable.DefaultImpls.move(this);
            }
    
            public static final c INSTANCE = (c)this;
    
            static 
            {
                new c();
            }
    
            private c()
            {
            }
        }
    
    
        public Outer()
        {
        }
    
        private String a;
    }
    

    jad -s .java Moveable.class

    public interface Moveable
    {
        public static final class DefaultImpls
        {
    
            public static void move(Moveable moveable)
            {
            }
        }
        public abstract void move();
    }
    
    

    jad -s .java Moveable$DefaultImpls.class

    public static final class Moveable$DefaultImpls
    {
    
        public static void move(Moveable moveable)
        {
        }
    }
    
    

    jad -s .java Outer$c.class

    public static final class Outer$c
        implements Moveable
    {
    
        public void move()
        {
            .DefaultImpls.move(this);
        }
    
        public static final Outer$c INSTANCE = (Outer$c)this;
    
        static 
        {
            new Outer$c();
        }
    
        private Outer$c()
        {
        }
    }
    
    

    2、使用object声明类似java中的匿名内部类

    interface Moveable{
       fun move(){
    
       }
    }
    
    
    class Outer{
       private var a:String?=null
       //被编译成匿名内部类
       private var c = object:Moveable {
           override fun move() {
               //接口的默认实现
               super.move()
               print(a)
           }
       }
    }
    
    编译后生成的class文件
    public static final class Outer$c$1
        implements Moveable
    {
    
        public void move()
        {
            efaultImpls.move(this);
            String s = Outer.access$getA$p(Outer.this);
            System.out.print(s);
        }
    
        final Outer this$0;
    
        Outer$c$1(Outer $outer)
        {
            this$0 = $outer;
            super();
        }
    }
    
    
    public final class Outer
    {
    
        public Outer()
        {
            c = new Moveable(this) {
    
                public void move()
                {
                    Moveable.DefaultImpls.move(this);
                    String s = a;
                    System.out.print(s);
                }
    
                final Outer this$0;
    
                
                {
                    this$0 = $outer;
                    super();
                }
            }
    ;
        }
    
        private String a;
        private c._cls1 c;
    
    
    }
    

    3、使用object声明单例对象

    object Singleton{
        fun f1() {
    
        }
    
        fun f2() {
    
        }
    }
    

    java代码

    public final class Singleton
    {
    
        public final void f1()
        {
        }
    
        public final void f2()
        {
        }
    
        private Singleton()
        {
        }
    
        public static final Singleton INSTANCE = (Singleton)this;
    
        static 
        {
            new Singleton();
        }
    }
    
    

    相关文章

      网友评论

          本文标题:Kotlin学习-1.object用法

          本文链接:https://www.haomeiwen.com/subject/otrvoftx.html