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

相关推荐
一点媛艺3 小时前
Kotlin函数由易到难
开发语言·python·kotlin
姑苏风3 小时前
《Kotlin实战》-附录
android·开发语言·kotlin
萌面小侠Plus12 小时前
Android笔记(三十三):封装设备性能级别判断工具——低端机还是高端机
android·性能优化·kotlin·工具类·低端机
wk灬丨16 小时前
Android Kotlin Flow 冷流 热流
android·kotlin·flow
晨曦_子画17 小时前
编程语言之战:AI 之后的 Kotlin 与 Java
android·java·开发语言·人工智能·kotlin
大福是小强17 小时前
005-Kotlin界面开发之程序猿初试Composable
kotlin·界面开发·桌面应用·compose·jetpack·可组合
&岁月不待人&19 小时前
Kotlin by lazy和lateinit的使用及区别
android·开发语言·kotlin
小白学大数据1 天前
正则表达式在Kotlin中的应用:提取图片链接
开发语言·python·selenium·正则表达式·kotlin
bytebeats2 天前
Kotlin 注解全面指北
android·java·kotlin
jzlhll1232 天前
kotlin android Handler removeCallbacks runnable不生效的一种可能
android·开发语言·kotlin