【Kotlin】匿名类和伴生类

1 匿名类

1)无继承

fun main() {
    var obj = object {
        var name: String = "zhang"

        override fun toString(): String {
            return name
        }
    }
    println(obj) // zhang
}

2)有继承

fun main() {
    var obj = object: People {
        var name: String = "zhang"

        override fun play() {
            println("play, $name")
        }
    }
    obj.play() // play, zhang
}

interface People {
    fun play()
}

2 单例

2.1 案例一

1)object 单例

fun main() {
    var singleton = Singleton
    println(singleton)
}

object Singleton {
    var name = "zhang"

    override fun toString(): String {
        return return name
    }
}

2)对应的 java 类

​ Singleton.java

public final class Singleton {
    private static String name;

    public static final Singleton INSTANCE;

    static {
        INSTANCE = new Singleton();
        name = "zhang";
    }

    private Singleton() {
    }

    public final String getName() {
        return name;
    }

    public final void setName(String var1) {
        name = var1;
    }

    public String toString() {
        return name;
    }
}

​ java 中调用如下。

Singleton singleton = Singleton.INSTANCE;
System.out.println(singleton);

2.2 案例二

1)object 单例

fun main() {
    var stu = Student.Tools.create("zhang")
    println(stu) // zhang
    println(Student.Tools.age) // 15
}

class Student(var name: String) {
    object Tools {
        var age: Int = 15

        fun create(name: String): Student {
            return Student(name)
        }
    }

    override fun toString(): String {
        return name
    }
}

2)对应的 java 类

​ Student.java

public final class Student {
    private String name;

    public Student(String name) {
        this.name = name;
    }

    public final String getName() {
        return this.name;
    }

    public final void setName(String var1) {
        this.name = var1;
    }
    
    public String toString() {
        return this.name;
    }

    public static final class Tools {
        private static int age;
        public static final Tools INSTANCE;

        static {
            INSTANCE = new Tools();
            age = 15;
        }

        private Tools() {
        }

        public final Student create(String name) {
            return new Student(name);
        }

        public final int getAge() {
            return age;
        }

        public final void setAge(int var1) {
            age = var1;
        }
    }
}

​ java 中调用如下。

Student stu = Student.Tools.INSTANCE.create("zhang");
System.out.println(stu);
System.out.println(Student.Tools.INSTANCE.getAge());

3 伴生类

3.1 伴生类应用

1)应用

fun main() {
    var stu = Student.create("zhang")
    println(stu) // zhang
    println(Student.age) // 15
}

class Student(var name: String) {
    companion object Tools {
        var age: Int = 15

        fun create(name: String): Student {
            return Student(name)
        }
    }

    override fun toString(): String {
        return name
    }
}

2)反编译后 java 类

​ Student.java

public final class Student {
    private String name;
    private static int age = 15;
    public static final Tools Tools = new Tools(();

    public Student(String name) {
        this.name = name;
    }

    public final String getName() {
        return this.name;
    }

    public final void setName(String var1) {
        this.name = var1;
    }

	public String toString() {
        return this.name;
    }

    public static final class Tools {
        private Tools() {
        }

		public final Student create(String name) {
            return new Student(name);
        }

        public final int getAge() {
            return Student.age;
        }

        public final void setAge(int var1) {
            Student.age = var1;
        }
    }
}

​ java 中调用如下。

Student stu = Student.Tools.create("zhang");
System.out.println(stu);
System.out.println(Student.Tools.getAge());

3.2 @JvmStatic 应用

​ 为方便 Java 中也可以直接调用 Student.create(),可以给伴生类中的函数添加 @JvmStatic 注解。

1)应用

class Student(var name: String) {
    companion object Tools {
        var age: Int = 15

        @JvmStatic
        fun create(name: String): Student {
            return Student(name)
        }
    }

    override fun toString(): String {
        return name
    }
}

​ java 中调用如下。

Student stu = Student.create("zhang");
System.out.println(stu);
System.out.println(Student.Tools.getAge());

2)反编译后 java 类

public final class Student {
    private String name;
    private static int age = 15;
    public static final Tools Tools = new Tools();

    public Student(String name) {
        this.name = name;
    }

    public static final Student create(String name) {
        return Tools.create(name);
    }

    public final String getName() {
        return this.name;
    }

    public final void setName(String var1) {
        this.name = var1;
    }

    public String toString() {
        return this.name;
    }

    public static final class Tools {
        private Tools() {
        }
		
        public final Student create(String name) {
            return new Student(name);
        }

        public final int getAge() {
            return Student.age;
        }

        public final void setAge(int var1) {
            Student.age = var1;
        }
    }
}

​ 说明:对比有无 @JvmStatic 注解反编译后的代码,发现有 @JvmStatic 注解时,反编译的代码里 Student 类中多了静态的 create 方法,因此在 Java 中可以直接调用 Student.create()。

​ 声明:本文转自【Kotlin】匿名类和伴生类

posted @ 2024-04-10 21:56  little_fat_sheep  阅读(68)  评论(0编辑  收藏  举报