【Kotlin】匿名类和伴生类

1 匿名类

1)无继承

Kotlin 复制代码
fun main() {
    var obj = object {
        var name: String = "zhang"

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

2)有继承

Kotlin 复制代码
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 单例

Kotlin 复制代码
fun main() {
    var singleton = Singleton
    println(singleton)
}

object Singleton {
    var name = "zhang"

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

2)对应的 java 类

​ Singleton.java

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 中调用如下。

java 复制代码
Singleton singleton = Singleton.INSTANCE;
System.out.println(singleton);

2.2 案例二

1)object 单例

Kotlin 复制代码
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

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 中调用如下。

java 复制代码
Student stu = Student.Tools.INSTANCE.create("zhang");
System.out.println(stu);
System.out.println(Student.Tools.INSTANCE.getAge());

3 伴生类

1)companion object

Kotlin 复制代码
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

Kotlin 复制代码
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 中调用如下。

java 复制代码
Student stu = Student.Tools.create("zhang");
System.out.println(stu);
System.out.println(Student.Tools.getAge());

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

相关推荐
修炼者1 天前
Kotlin中的Flow流
android·kotlin
aqi001 天前
FFmpeg开发笔记(九十二)基于Kotlin的开源Android推流器StreamPack
android·ffmpeg·kotlin·音视频·直播·流媒体
饕餮争锋2 天前
Kotlin: [Internal Error] java.lang.NoSuchFieldError: FILE_HASHING_STRATEGY
java·kotlin
用户69371750013842 天前
14.Kotlin 类:类的形态(一):抽象类 (Abstract Class)
android·后端·kotlin
G_dou_3 天前
KMP & OpenHarmony 实现二分查找
kotlin·鸿蒙
方白羽3 天前
Android多层嵌套RecyclerView滚动
android·java·kotlin
方白羽3 天前
Kotlin遇上Java 静态方法
android·java·kotlin
用户69371750013843 天前
11.Kotlin 类:继承控制的关键 ——final 与 open 修饰符
android·后端·kotlin
用户69371750013843 天前
10.Kotlin 类:延迟初始化:lateinit 与 by lazy 的对决
android·后端·kotlin
KotlinKUG贵州3 天前
SpringGateway-MVC对SSE转发出现阻塞响应问题的分析和解决
spring·spring cloud·kotlin