快速上手kotlin接口抽象类及泛型

Posted super码王

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了快速上手kotlin接口抽象类及泛型相关的知识,希望对你有一定的参考价值。

接口定义

        kotlin规定所有的接口属性和函数实现都要使用 override 关键字,接口中定义的函数并不需要open关键字修饰,他们默认就是open的

代码展示:

package com.wustyq.kotlinstudy

interface Movable 
    var maxSpeed: Int
    var wheels: Int

    fun move(movable: Movable): String


class Car(name: String, override var wheels: Int = 4) : Movable 
    override var maxSpeed: Int
        get() = TODO("Not yet implemented")
        set(value) 

    override fun move(movable: Movable): String 
        TODO("Not yet implemented")
    
  • 接口里的属性和方法都得实现
  • 属性实现可以选择在构造方法里,也可以不在构造方法里,如果不在构造方法里,就得设置get和set方法

默认实现

        我们也可以在接口里提供默认属性的getter方法和函数实现

代码示例:

package com.wustyq.kotlinstudy

interface Movable 
    val maxSpeed: Int
        get() = (0..4).shuffled()
            .last()
    var wheels: Int

    fun move(movable: Movable): String


class Car(name: String, override var wheels: Int = 4) : Movable 
    override var maxSpeed: Int
        get() = super.maxSpeed
        set(value) 

    override fun move(movable: Movable): String 
        TODO("Not yet implemented")
    
  • 接口里属性修饰符是val,接口里属性必须提供get方法
  • 接口里属性修饰符是val,实现类可以是val 或 var,如果是val就不用set方法,如果是var,就得set和get一起

抽象类

        要定义一个抽象类,你需要在定义之前加上 abstract 关键字,除了具体的函数实现,抽象类也可以包含抽象函数---只有定义,没有函数实现。

代码示例:

package com.wustyq.kotlinstudy

abstract class Gun(val range: Int) 
    //抽象类里面既有具体实现方法
    protected fun doSomething() 
        println("doSomething")
    

    //也存在抽象方法,继承该抽象类的类要实现这个方法
    abstract fun pullTrigger(): String


class AK47(val price: Int) : Gun(range = 500) 
    override fun pullTrigger(): String 
        TODO("Not yet implemented")
    

定义泛型类型

 代码展示:

package com.wustyq.kotlinstudy

class MagicBox<T>(item: T) 
    private var subject: T = item


class Boy(val name: String, val age: Int)

class Dog(val weight: Int)

fun main() 
    val box1: MagicBox<Boy> = MagicBox(Boy("Jack", 20))
    val box2: MagicBox<Dog> = MagicBox(Dog(20))

泛型函数

代码展示: 

package com.wustyq.kotlinstudy

class MagicBox<T>(item: T) 
    var available = false
    private var subject: T = item

    fun fetch(): T? 
        return subject.takeIf  available 
    


class Boy(val name: String, val age: Int)

class Dog(val weight: Int)

fun main() 
    val box1: MagicBox<Boy> = MagicBox(Boy("Jack", 20))
    val box2: MagicBox<Dog> = MagicBox(Dog(20))
    box1.available = true
    println(box1.fetch()?.name)

效果展示:

多泛型参数

代码示例: 

package com.wustyq.kotlinstudy

import javax.security.auth.Subject

class MagicBox<T>(item: T) 
    var available = false
    private var subject: T = item

    fun fetch(): T? 
        return subject.takeIf  available 
    

    fun <R> fetch(subjectModFunction: (T) -> R): R? 
        return subjectModFunction(subject).takeIf  available 
    


class Boy(val name: String, val age: Int)

class Dog(val weight: Int)

fun main() 
    val box1: MagicBox<Boy> = MagicBox(Boy("Jack", 20))
    val box2: MagicBox<Dog> = MagicBox(Dog(20))
    box1.available = true
    println(box1.fetch()?.age)

    val boy3 = box1.fetch 
        Boy(it.name, it.age + 10)
    
    println(boy3?.age)

效果展示:

泛型类型约束

 代码示例: 

package com.wustyq.kotlinstudy

import javax.security.auth.Subject

class MagicBox<T : Human>(item: T) 
    var available = false
    private var subject: T = item

    fun fetch(): T? 
        return subject.takeIf  available 
    

    fun <R> fetch(subjectModFunction: (T) -> R): R? 
        return subjectModFunction(subject).takeIf  available 
    


open class Human(val age: Int)

class Boy(val name: String, age: Int) : Human(age)

class Dog(val weight: Int)

fun main() 
    val box1: MagicBox<Boy> = MagicBox(Boy("Jack", 20))
    val box2: MagicBox<Dog> = MagicBox(Dog(20))
    box1.available = true
    println(box1.fetch()?.age)

    val boy3 = box1.fetch 
        Boy(it.name, it.age + 10)
    
    println(boy3?.age)

 效果展示:

以上是关于快速上手kotlin接口抽象类及泛型的主要内容,如果未能解决你的问题,请参考以下文章

ADO.Net创建数据模型和数据访问类及泛型集合

Kotlin学习笔记——接口抽象类泛型扩展集合操作符与Java互操作性单例

Kotlin中接口抽象类泛型out(协变)in(逆变)reified关键字的详解

快速上手 Kotlin 开发系列之操作符

C#规范整理·泛型委托事件

C#规范整理·泛型委托事件