泛型

参考文档:Kotlin 泛型(修订版)

名词、概念、示例,太密集了,我看得很吃力。所以,写了一套测试代码。下次,忘了怎么写,就翻出来看一眼。
大不了,从上到下,挨着排地再写一遍,所有的概念,也就懂了。

import org.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.describe
import org.jetbrains.spek.api.dsl.it
import org.jetbrains.spek.api.dsl.on
import kotlin.test.assertEquals


object GenericSpec : Spek({
    describe("test out") {
        class Zoo(val t: T)
        it("readOnly") {
            val zoo: Zoo = Zoo(Dog())
            assertEquals("dog", zoo.t.name)
//            zoo.t = Bird() //不能这么写,编译的时候会报错。即便,使用var修饰参数t,也不允许调用setter
        }

    }
    describe("test in") {
        class Zoo(var t: T)
        it("writeOnly") {
            val zoo: Zoo = Zoo(Dog())
            zoo.t = Bird() // 必须使用var修饰参数,否则,不能调用setter
            val obj = zoo.t
            if (obj is Bird) { //不能直接读。必须确定class,才能调用属性、方法
                assertEquals("bird", obj.name)
            }
        }
    }

    //不推荐下面的写法,很难看懂。建议,了解即可。但,不能否认,代码看起来很高级的样子
    describe("test *") {
        on("can be read") {
            it("use out Number") {
                val list: List = listOf(1, 2.0)
                assertEquals(Int::class, list[0]!!::class)
                assertEquals(Double::class, list[1]!!::class)
            }
            it("use out Int") {
                val list: List = listOf(1, 2.0.toInt()) //必须强转,否则,编译失败
                assertEquals(Int::class, list[0]!!::class)
                assertEquals(Int::class, list[1]!!::class)
                assertEquals(2, list[1])
            }
            it("use *") {
                val list: List<*> = listOf(1, 2.0)
                assertEquals(Int::class, list[0]!!::class)
                assertEquals(Double::class, list[1]!!::class)
            }
        }
        on("can be wrote") {
            //下面的两种写法,同样是等价的
            it("use in") {
                val list: MutableList = mutableListOf(1, 2.0)
                assertEquals(Int::class, list[0]!!::class)
                assertEquals(Double::class, list[1]!!::class)
            }
            it("use in Int") {
                val list: MutableList = mutableListOf(1, 2.0) //有点神奇了...没关系,再写一个测试
                assertEquals(Int::class, list[0]!!::class)
                assertEquals(Double::class, list[1]!!::class)
                assertEquals(2.0, list[1])
            }
            it("use in Dog") {
                val list: MutableList = mutableListOf(Dog(), Animal())
                assertEquals(Dog::class, list[0]!!::class)
                assertEquals(Animal::class, list[1]!!::class)
                assertEquals("animal", (list[1] as Animal).name) //这就有意思了。一般情况用不上。遇到了,看这个例子吧。看完,就会写了
            }
            it("use *") {
                val list: List<*> = mutableListOf(1, 2.0)
                assertEquals(Int::class, list[0]!!::class)
                assertEquals(Double::class, list[1]!!::class)
            }
        }
    }

    describe("test extend") {
        class Zoo(val t: T)
        it("when T is Animal") {
            val zoo: Zoo = Zoo(Dog())
            assertEquals("dog", zoo.t.name)
        }
        it("when T is not Animal") {
            //            val zoo: Zoo = Zoo(1) //编译错误
//            assertEquals(1, zoo.t)
        }
    }
    describe("test extends multiple parent classes") { //按编译器的提示写吧。反正,我不想知道为什么
        class Zoo1(val t: T) where T : Number, T : Cloneable //编译通过
        //class Zoo2(val t: T) where T : Animal, T : Dog //编译失败
        //class Zoo3(val t: T) where T : Dog, T : Bird //编译失败
        class Zoo4(val t: T) where T : Animal, T : Cloneable //编译通过
    }
})

open class Animal(val name: String = "animal")
class Dog(name: String = "dog") : Animal(name)
class Bird(name: String = "bird") : Animal(name)

你可能感兴趣的:(泛型)