zl程序教程

您现在的位置是:首页 >  Java

当前栏目

kotlin基础--对象、接口、抽象类

2023-04-18 12:32:30 时间

上次介绍了kotlin的类定义与初始化,接下来学习对象、接口、抽象类

一、对象

1.object关键字

object,类似Java中的静态 三种使用方式:

1.1 对象声明

对应Java中的单例类,只会在内存中实例化一次

object Const {
    init {
        println("init")
    }

    fun getConfig(): String {
        return "config"
    }
}

fun main() {
    println(Const.getConfig())
    println(Const.getConfig())
}

结果: init config config

1.2 对象表达式

Java中的匿名类 java写法如下:

    static class Man {
        void doSomthing() {

        }
    }

    public static void main(String[] args) {
        Man my = new Man() {
            @Override
            void doSomthing() {
                super.doSomthing();
            }
        };
    }

kotlin写法:

open class Man {
    open fun doSomthing() = ""
}

fun main() {
    var superMan = object : Man() {
        override fun doSomthing(): String {
            return "super Man protected earth"
        }
    }

    println(superMan.doSomthing())
}
1.3 伴生对象

如果你想要某个对象和一个类实例化绑定在一起,可以考虑伴生对象,使用 companion object 可以在一个类里定义一个伴生对象,一个类只能有一个伴生对象, 伴生对象也是静态的,只会在类实例化或调用伴生对象中的内容(对象和函数)时实例化一次

class Const {
    init {
        println("init Const")
    }

    companion object {
        init {
            println("init companion object")
        }

        fun getConfig(): String {
            return "config"
        }
    }

}

fun main() {
    val const = Const()
    
    println(Const.getConfig())
    println(Const.getConfig())
}

结果: init companion object init Const config config

2.嵌套类

和Java中的内部类类似,如果一个类只对另一个类有用,那么使用内部类是合理的

class Man {
    class BatMan {
        fun introduce() = "i'm Batman"
    }

    class IronMan {
        fun introduce() = "i'm Ironman"
    }
}

fun main() {
    println(Man.BatMan().introduce())
}
3.数据类

数据类是用来存储数据的类,它会自动实现toString、hashCode、equals的个性化实现

class NormalClz(var x: Int, var y: Int) {

}

data class DataClz(var x: Int, var y: Int) {
    var z: Int = 40
}

fun main() {
    val normal = NormalClz(10, 10)
    println(normal)

    val data = DataClz(10, 10)
    println(data)

}

结果: com.aruba.mykotlinapplication.NormalClz@5e481248 DataClz(x=10, y=10)

注意:数据类自动实现的个性化只对主构造函数里的定义的参数起作用

4.copy函数

数据类还提供了copy函数,用来复制一个对象

data class DataClz(var x: Int, var y: Int) {
    var z: Int = 40

    override fun toString(): String {
        return "DataClz : $x $y $z"
    }

    constructor(_x: Int) : this(_x, 20) {
        //copy函数不会赋值
        this.z = 20
    }
}

fun main() {
    val data = DataClz(10, 10)
    println(data.copy(20))
}

结果: DataClz : 20 10 40

注意:copy函数不会复制次构造函数中的赋值

5.解构声明

前面我们已经使用过解构语法了,如果想要在自己定义的类中使用,需要operate关键字, 并声明component1、component2...组件函数,函数名不能擅自改动

class NormalClz(var x: Int, var y: Int) {
    operator fun component1() = x
    operator fun component2() = y
}

fun main() {
    val normal = NormalClz(20, 30)

    val (x, y) = normal
    println("$x $y")
}

数据类会自动为所有在主构造函数内定义的属性进行解构声明

data class DataClz(var x: Int, var y: Int) {
}

fun main() {
    val data = DataClz(20, 30)

    val (x, y) = data
    println("$x $y")
}
6.使用数据类的条件
7.运算符重载

之前使用集合是我们可以直接使用 "+" 、"-" 等来添加和删除元素 和c++一样,kotlin也支持运算符重载,只需要重载下面的函数就可以实现了:

8.枚举类

用来定义常量集合的一种特殊类

enum class Position {
    east,
    west,
    south,
    north
}

也可以定义函数

enum class Position(
    val data: DataClz
) {
    east(DataClz(1, 0)),
    west(DataClz(-1, 0)),
    south(DataClz(0, -1)),
    north(DataClz(0, 1));

    fun updatePosition(upData: DataClz) =
        DataClz(
            this.data.x + upData.x,
            upData.y + data.y
        )
}

fun main() {
    println(Position.east.data)
    println(Position.east.updatePosition(DataClz(10, 10)))
}
9.代数数据类型

可以用来表示一种子类型的闭集,枚举类就是一种简单的代数数据类型(ADT)

enum class Position {
    east,
    west,
    south,
    north
}

class Treasure(var position: Position) {
    fun find() = when (position) {
        Position.east -> "不在东边"
        Position.west -> "不在西边"
        Position.south -> "不在南边"
        Position.north -> "你在北边找到了宝藏"
    }
}


fun main() {
    println(Treasure(Position.north).find())
}

结果: 你在北边找到了宝藏

10.密封类

对于更复杂的ADT,可以使用密封类,使用sealed修饰类,来实现更加复杂的定义,密封类可以更灵活的控制某个子类型 子类型必须和它定义在同一个文件里

sealed class Position {
    //使用object,防止重复创建
    object east : Position()
    object west : Position()
    object south : Position()
    class north(var item: String) : Position()
}

class Treasure(var position: Position) {
    fun find() = when (position) {
        is Position.east -> "不在东边"
        is Position.west -> "不在西边"
        is Position.south -> "不在南边"
        is Position.north -> "你在北边找到了:${(position as Position.north).item}"
    }
}


fun main() {
    println(Treasure(Position.west).find())
    println(Treasure(Position.north("肥皂")).find())
}

结果: 不在西边 你在北边找到了:肥皂

二、接口

和Java差不多,用interface定义,实现接口的方法必须有override修饰符

interface Callback {
    fun callback()
}

class Impl() : Callback {
    override fun callback() {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }
}

接口也可以定义函数实现和getter函数

interface Callback {
    val number: Int
        get() = (0..500).shuffled().first()

    fun callback() = "haha"
}

class Impl() : Callback {
    override fun callback(): String {
        return super.callback()
    }
}

三、抽象类

和Java一样,可以用abstract修饰类和方法

abstract class AbstractClass {
    abstract fun callback(): String
}

class ImplClz() : AbstractClass() {
    override fun callback(): String {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }
}