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

相关推荐
居居飒13 小时前
Android学习(四)-Kotlin编程语言-for循环
android·学习·kotlin
刘争Stanley2 天前
如何高效调试复杂布局?Layout Inspector 的 Toggle Deep Inspect 完全解析
android·kotlin·android 15·黑屏闪屏白屏
sickworm陈浩2 天前
Java 转 Kotlin 系列:究竟该不该用 lateinit?
android·kotlin
droidHZ3 天前
Compose Multiplatform 之旅—声明式UI
android·kotlin
zhangphil3 天前
Android基于Path的addRoundRect,Canvas剪切clipPath简洁的圆角矩形实现,Kotlin(1)
android·kotlin
alexhilton5 天前
Android技巧:学习使用GridLayout
android·kotlin·android jetpack
zhangphil6 天前
Android使用PorterDuffXfermode的模式PorterDuff.Mode.SRC_OUT实现橡皮擦,Kotlin(1)
android·kotlin
IH_LZH7 天前
OkHttp源码分析:分发器任务调配,拦截器责任链设计,连接池socket复用
android·java·okhttp·kotlin
casual_clover8 天前
Android之RecyclerView显示数据列表和网格
android·kotlin
氤氲息9 天前
导入kotlin
android·开发语言·kotlin