接口定义

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")
}
}

定义泛型类型

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

代码展示:

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))
}

泛型函数

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

代码展示:

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)
}

效果展示:

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

多泛型参数

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

代码示例:

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)
}

效果展示:

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

泛型类型约束

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

代码示例:

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接口、抽象类及泛型_android_07