【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】匿名类和伴生类

相关推荐
长亭外的少年9 小时前
Kotlin 编译失败问题及解决方案:从守护进程到 Gradle 配置
android·开发语言·kotlin
JIAY_WX9 小时前
kotlin
开发语言·kotlin
麦田里的守望者江16 小时前
KMP 中的 expect 和 actual 声明
android·ios·kotlin
菠菠萝宝1 天前
【YOLOv8】安卓端部署-1-项目介绍
android·java·c++·yolo·目标检测·目标跟踪·kotlin
恋猫de小郭1 天前
Kotlin Multiplatform 未来将采用基于 JetBrains Fleet 定制的独立 IDE
开发语言·ide·kotlin
枫__________1 天前
kotlin 协程 job的cancel与cancelAndJoin区别
android·开发语言·kotlin
鸠摩智首席音效师2 天前
如何在 Ubuntu 上配置 Kotlin 应用环境 ?
linux·ubuntu·kotlin
jikuaidi6yuan3 天前
Java与Kotlin在鸿蒙中的地位
java·kotlin·harmonyos
liulanba3 天前
Kotlin的data class
前端·微信·kotlin
小白学大数据4 天前
使用OkHttp进行HTTPS请求的Kotlin实现
爬虫·python·okhttp·https·kotlin