农业网站建设策划书,查找北京国互网网站建设,截屏的图片wordpress不能显示,免费注册店铺位置文章目录 定义泛型属性泛型函数泛型类或接口 where 声明多个约束泛型具体化in、out 限制泛型输入输出 定义
有时候我们会有这样的需求#xff1a;一个类可以操作某一类型的对象#xff0c;并且限定只有该类型的参数才能执行相关的操作。 如果我们直接指定该类型Int#xff… 文章目录 定义泛型属性泛型函数泛型类或接口 where 声明多个约束泛型具体化in、out 限制泛型输入输出 定义
有时候我们会有这样的需求一个类可以操作某一类型的对象并且限定只有该类型的参数才能执行相关的操作。 如果我们直接指定该类型Int确实可以实现该操作但是换一个类型就得再重新定义一个类
class MyArray {fun add(int: Int) {}fun delete(int: Int) {}
}fun main() {val myArray MyArray()myArray.add(1)myArray.delete(1)
}如果我们将类型改为Any又会导致可操作参数太过笼统还容易出现各种问题
class MyArray {fun add(any: Any) {}fun delete(any: Any) {}
}fun main() {val myArray MyArray()myArray.add(1)myArray.delete(abc)
}这时候我们需要引入泛型。泛型的定义方式如下
泛型名[: 泛型所属类型], ...一般情况下泛型名会是单个的大写字母很多时候是T而泛型所属类型可以指定泛型是某个类/接口或者其子类/实现当然单独这样写做不了什么请继续往下看
T
T: Runnable
K, V泛型属性
在 Kotlin 中可以为一个类定义扩展属性
class MyClassval MyClass.name get() MyClass如果我们要指定是任何类都有的属性则可以泛型。 泛型属性只能用在扩展声明时需要将泛型定义放置在val|var后变量名前。
val T T.name get() Anyfun main() {val int 1print(int.name)
}Any也可以限制该泛型是Runnable接口或其实现。
val T: Runnable T.name get() Runnablefun main() {val int 1// print(int.name) 不可访问print(Runnable { }.name)
}Runnable泛型函数
我们也可以为函数泛型与泛型扩展属性类似泛型定义需要写在fun和函数名之间意外发现此时as如果无法转换不会抛出异常而是返回any
fun T cast(any: Any) any as Tfun main() {castListDouble(listOf(1, 2)).forEach { print(it); print(, ) }
}1.0, 2.0, Note泛型可以作为函数接收者、参数、返回值类型的一部分。如果一个泛型函数在调用时可以通过传入的参数类型或返回值声明确定泛型的类型则调用时泛型的类型可以缺省否则需要指明其类型函数名泛型类型, ...()泛型类与此类似。 声明两个泛型涉及到的 Kotlin 一些便捷的习语我们将在后续文章学习
fun K, V hashMap(vararg pairs: PairK, V) HashMapK, V().apply { putAll(pairs) }fun main() {print(hashMap(key to 1))
}{key1}泛型扩展函数
// List 类是一个泛型类
// 可以将它的泛型指定为函数的泛型
fun T ListT.print() print(this)fun main() {listOf(1, 2, 3).print()
}[1, 2, 3]fun T: ComparableInt T.isGreaterZero() this 0fun T: ComparableInt T.isGreaterZero() this 0fun main() {val int 1// Int 实现了 ComparableInt// 因此可以调用print(int.isGreaterZero())
}true作为返回值
fun T Any.toType() this as Tfun main() {// 没有指定变量类型无法推断泛型的类型// 因此调用时需要声明泛型类型val doubleList listOfString().toTypeListDouble()val intList: ListDouble listOfString().toType()
}泛型类或接口
在定义泛型类或接口时需要将泛型定义放置在类名之后括号之前。 泛型函数与接口是类似的这里以类为例。
class GroupT(vararg elements: T) {private val value mutableListOf(*elements)fun add(element: T) value.add(element)fun remove(element: T) value.remove(element)// 重写 toString调用 print 时即可打印出列表override fun toString(): String value.toString()
}fun main() {val group Group(1, 2, 3) // GroupInt(1, 2, 3)println(group)group.remove(2)println(group)group.remove(4)print(group)
}[1, 2, 3]
[1, 3]
[1, 3]Notevararg用于定义数量不定的参数vararg element: Int表示你可以传入任意数量0…无限大的Int类型在内部访问element会获取到一个数组ArrayInt而使用*可以将其展开为vararg也就是原始传入的任意数量的Int可以用于传入另一个需要vararg的函数。 使用*可以指代所有泛型下方例子中相当于Any?*无法用在函数调用上
fun List*.print() print(this)fun main() {listOf(1, 2, 3).print()println()listOf(a, b, c).print()
}[1, 2, 3]
[a, b, c]where 声明多个约束
我们可以使用T: 类型来约束泛型为某一类型或其子类。但有时候我们想要将其约束于多个类型可以使用where来实现此时T: 类型中的类型也应该一并移到where处 我们先定义一个接口和一个类后面的例子会反复用到
interface MyInterfaceclass MyClass : Runnable, MyInterface {override fun run() {}
}泛型扩展属性 // 只有实现了 Runnable 和 MyInterface 才能拥有此扩展属性
val T T.id where T: Runnable, T: MyInterfaceget() 123fun main() {MyClass().id
}泛型函数 // 只有实现了 Runnable 和 MyInterface 才能拥有此扩展函数
fun T T.getId() where T : Runnable, T : MyInterface 123// 只有泛型 T 实现了 Runnable 和 MyInterface 才能使用此函数
fun T getName() where T : Runnable, T : MyInterface RunnableMyInterfacefun main() {MyClass().getId()getNameMyClass()
}泛型类或接口 // 只有实现了 Runnable 和 MyInterface 才能作为构造参数 value 传入
class MyContainerT(val value: T) where T : Runnable, T : MyInterfacefun main() {MyContainer(MyClass())
}泛型具体化
有时候我们需要判断泛型的类型会想到用类型::class获取对应的KClass进行对比。而当该类型为泛型时我们需要将其具象化reified通常该声明只能用于函数并且需要与inline搭配使用
inline fun reified T T.isInt() T::class Int::classfun main() {println(1.isInt())print(false.isInt())
}true
falsein、out 限制泛型输入输出
in和out修饰泛型只能用在类或接口。 当一个泛型被in修饰时表明该类型只能作为对象属性、方法的输入赋值、传参
class Groupin T(vararg elements: T) {// 因为泛型声明为 in属性类型中包含 T 可见性只能为 privateprivate val value mutableListOf(*elements)fun add(element: T) {}// 因为泛型声明为 in不能定义返回 T 类型的方法// fun get(index: Int) value[index]
}当一个泛型被in修饰时表明该类型只能作为对象属性、方法的输出获取值、函数返回
class Groupout T(vararg elements: T) {// 因为泛型声明为 out而 MutableListT 具有 in 和 out// 若将其公开将会允许 in T例如 value.add(element: T)// 属性类型中包含 T 可见性只能为 privateprivate val value mutableListOf(*elements)// 因为泛型声明为 out不能作为参数类型// fun add(element: T) {}fun get(index: Int) value.get(index)
}Note注意是针对对象而言如果是一个类声明了out泛型仍然可以将该泛型作为构造函数中的参数类型。 class Groupout T(vararg elements: T)