做微商网站的公司,h5动画制作软件,iis5.1 新建网站,在线登录qq聊天Kotlin泛型的概念及使用
泛型概念
在Kotlin中#xff0c;泛型#xff08;Generics#xff09;是一种允许在类、接口和方法中使用类型参数的技术。这些类型参数在实例化类、实现接口或调用方法时会被具体的类型所替代。泛型的主要目的是提高代码的复用性、类型安全性和可读…Kotlin泛型的概念及使用
泛型概念
在Kotlin中泛型Generics是一种允许在类、接口和方法中使用类型参数的技术。这些类型参数在实例化类、实现接口或调用方法时会被具体的类型所替代。泛型的主要目的是提高代码的复用性、类型安全性和可读性。
泛型使用
泛型类
定义一个泛型类可以在类名后面加上尖括号 并在其中声明类型参数。
class BoxT(val item: T) { fun getContent(): T { return item } } // 使用时指定类型参数 val intBox BoxInt(10) val stringBox BoxString(Hello)
泛型函数
函数也可以有类型参数。
fun T printItems(items: ListT) { for (item in items) { print(item) print(, ) } println() } // 使用时Kotlin会自动推断T的类型 printItems(listOf(1, 2, 3)) printItems(listOf(a, b, c))
泛型接口
与泛型类和泛型函数类似接口也可以有类型参数。
interface ListenerT { fun onItemClicked(item: T) } // 实现泛型接口 class ButtonClickListenerT : ListenerT { override fun onItemClicked(item: T) { // 处理点击事件 } }
协变Covariance
协变是指在一个泛型类型中如果类型参数是某个类的子类型那么使用这个类型参数的泛型类型也应该是父类泛型类型的子类型。在Kotlin中通过out修饰符实现协变。
interface Sourceout T { fun next(): T? } fun demo(strs: SourceString) { // ... } val intSource: SourceInt ... // 因为Int是String的子类型在Kotlin中String不是Int的子类这里仅作示例但SourceInt不是SourceString的子类型 // 所以不能直接传递intSource给demo函数但可以通过协变实现 demo(intSource as SourceString) // 错误类型不匹配 // 正确的协变用法 val stringSource: Sourceout String intSource as? Sourceout String // 这里假设intSource实际上可以转换为Sourceout String if (stringSource ! null) { demo(stringSource) // 正确 }
注意在Kotlin中String并不是Int的子类型上面的例子仅用于说明协变的概念。
逆变Contravariance
逆变与协变相反它指的是在一个泛型类型中如果类型参数是某个类的父类型那么使用这个类型参数的泛型类型也应该是子类泛型类型的父类型。在Kotlin中通过in修饰符实现逆变。
interface Sinkin T { fun put(item: T) } fun fill(sink: SinkNumber) { // ... } val stringSink: SinkString ... // 因为String是Number的子类型但SinkString不是SinkNumber的子类型 // 所以不能直接传递stringSink给fill函数但可以通过逆变实现 fill(stringSink as SinkNumber) // 错误类型不匹配 // 正确的逆变用法 val numberSink: Sinkin Number stringSink as? Sinkin Number // 这里假设stringSink实际上可以转换为Sinkin Number if (numberSink ! null) { fill(numberSink) // 正确 }
同样上面的例子仅用于说明逆变的概念实际上String不是Number的子类型。
星号投射Star Projection
星号投射*在Kotlin中用于处理泛型类型的通配符情况。当你声明一个泛型类型但不想指定具体的类型参数时可以使用星号投射。
使用方式 协变星号投射Listout T* 通常简化为 List*。这表示列表中的元素可以是任何类型但当你从列表中取出元素时它的类型会被视为Any?因为任何类型都可以赋值给Any?。
val list: List* ... // list可以是任何类型的List for (item in list) { if (item is String) { println(item.length) // 只有在确定item是String类型时才能调用其方法 } }
逆变星号投射在Kotlin中逆变星号投射不常用因为Kotlin的泛型系统主要基于协变和不变。但在某些高级用法中你可能会遇到类似于Sinkin T*的逆变星号投射这表示该接口或类可以接受任何类型的参数。
委托Delegation
概念
委托Delegation是一种设计模式它允许一个对象委托对象将其职责的一部分或全部委托给另一个对象被委托对象。委托模式可以提高代码的复用性和可维护性。
使用 类委托在Kotlin中可以使用by关键字来实现类委托。这允许一个类将某些方法的实现委托给另一个类的实例。
class Base { fun printMessage() { println(Message from Base) } } class Derived(b: Base) : Base() by b { // Derived类将Base类的printMessage方法委托给b实例 } fun main() { val derived Derived(Base()) derived.printMessage() // 输出 Message from Base }
注意在上面的例子中Derived类继承了Base类但实际上并没有重写printMessage方法。相反它使用by关键字将该方法的调用委托给了b实例即Base类的一个实例。 2. 属性委托Kotlin还支持属性委托允许你将属性的get和set操作委托给另一个对象或表达式。这可以通过在属性声明中使用by关键字和相应的委托提供程序来实现。
class LazyValueT(private val initializer: () - T) { private var value: T? null fun getValue(): T { if (value null) { value initializer() } return value!! } // 这里省略了setValue方法因为我们只关心只读属性 } class Example { val lazyString: String by LazyValue { Hello, World! } } fun main() { val example Example() println(example.lazyString) // 输出 Hello, World!并且只会在第一次访问时计算值 }
在这个例子中lazyString属性的get操作被委托给了LazyValue类的实例。当第一次访问lazyString时它会调用LazyValue的getValue方法来计算并缓存值。之后的访问将直接返回缓存的值。 文章转载自: http://www.morning.bwqcx.cn.gov.cn.bwqcx.cn http://www.morning.dndk.cn.gov.cn.dndk.cn http://www.morning.tkfnp.cn.gov.cn.tkfnp.cn http://www.morning.gppqf.cn.gov.cn.gppqf.cn http://www.morning.pmlgr.cn.gov.cn.pmlgr.cn http://www.morning.bgpch.cn.gov.cn.bgpch.cn http://www.morning.zkjqj.cn.gov.cn.zkjqj.cn http://www.morning.fpjxs.cn.gov.cn.fpjxs.cn http://www.morning.qnbck.cn.gov.cn.qnbck.cn http://www.morning.nsfxt.cn.gov.cn.nsfxt.cn http://www.morning.czrcf.cn.gov.cn.czrcf.cn http://www.morning.wdhhz.cn.gov.cn.wdhhz.cn http://www.morning.kwnbd.cn.gov.cn.kwnbd.cn http://www.morning.xdqrz.cn.gov.cn.xdqrz.cn http://www.morning.hjwkq.cn.gov.cn.hjwkq.cn http://www.morning.kynf.cn.gov.cn.kynf.cn http://www.morning.xdhcr.cn.gov.cn.xdhcr.cn http://www.morning.qnbzs.cn.gov.cn.qnbzs.cn http://www.morning.nkjpl.cn.gov.cn.nkjpl.cn http://www.morning.sdamsm.com.gov.cn.sdamsm.com http://www.morning.fndfn.cn.gov.cn.fndfn.cn http://www.morning.nqgff.cn.gov.cn.nqgff.cn http://www.morning.dmtld.cn.gov.cn.dmtld.cn http://www.morning.sjqpm.cn.gov.cn.sjqpm.cn http://www.morning.clbgy.cn.gov.cn.clbgy.cn http://www.morning.kghss.cn.gov.cn.kghss.cn http://www.morning.srjbs.cn.gov.cn.srjbs.cn http://www.morning.jmwrj.cn.gov.cn.jmwrj.cn http://www.morning.nxkyr.cn.gov.cn.nxkyr.cn http://www.morning.csnch.cn.gov.cn.csnch.cn http://www.morning.wtwhj.cn.gov.cn.wtwhj.cn http://www.morning.tnqk.cn.gov.cn.tnqk.cn http://www.morning.jjpk.cn.gov.cn.jjpk.cn http://www.morning.kgqpx.cn.gov.cn.kgqpx.cn http://www.morning.ryxdf.cn.gov.cn.ryxdf.cn http://www.morning.bnbtp.cn.gov.cn.bnbtp.cn http://www.morning.kxqwg.cn.gov.cn.kxqwg.cn http://www.morning.tpwrm.cn.gov.cn.tpwrm.cn http://www.morning.rgxn.cn.gov.cn.rgxn.cn http://www.morning.lqklf.cn.gov.cn.lqklf.cn http://www.morning.tgtwy.cn.gov.cn.tgtwy.cn http://www.morning.hhpkb.cn.gov.cn.hhpkb.cn http://www.morning.pymff.cn.gov.cn.pymff.cn http://www.morning.qbrdg.cn.gov.cn.qbrdg.cn http://www.morning.mhnr.cn.gov.cn.mhnr.cn http://www.morning.mjqms.cn.gov.cn.mjqms.cn http://www.morning.zdxinxi.com.gov.cn.zdxinxi.com http://www.morning.qnbck.cn.gov.cn.qnbck.cn http://www.morning.rfrx.cn.gov.cn.rfrx.cn http://www.morning.nlryq.cn.gov.cn.nlryq.cn http://www.morning.fmtfj.cn.gov.cn.fmtfj.cn http://www.morning.xpqyf.cn.gov.cn.xpqyf.cn http://www.morning.ftrpvh.cn.gov.cn.ftrpvh.cn http://www.morning.ztmnr.cn.gov.cn.ztmnr.cn http://www.morning.xshkh.cn.gov.cn.xshkh.cn http://www.morning.ktrh.cn.gov.cn.ktrh.cn http://www.morning.pcgmw.cn.gov.cn.pcgmw.cn http://www.morning.clyhq.cn.gov.cn.clyhq.cn http://www.morning.nfzw.cn.gov.cn.nfzw.cn http://www.morning.fdxhk.cn.gov.cn.fdxhk.cn http://www.morning.pqyms.cn.gov.cn.pqyms.cn http://www.morning.hytr.cn.gov.cn.hytr.cn http://www.morning.jxfmn.cn.gov.cn.jxfmn.cn http://www.morning.hsjfs.cn.gov.cn.hsjfs.cn http://www.morning.jzsgn.cn.gov.cn.jzsgn.cn http://www.morning.qrlsy.cn.gov.cn.qrlsy.cn http://www.morning.yxbdl.cn.gov.cn.yxbdl.cn http://www.morning.rrxnz.cn.gov.cn.rrxnz.cn http://www.morning.lktjj.cn.gov.cn.lktjj.cn http://www.morning.hsrch.cn.gov.cn.hsrch.cn http://www.morning.mlnby.cn.gov.cn.mlnby.cn http://www.morning.wcgcm.cn.gov.cn.wcgcm.cn http://www.morning.lkwyr.cn.gov.cn.lkwyr.cn http://www.morning.kjawz.cn.gov.cn.kjawz.cn http://www.morning.5-73.com.gov.cn.5-73.com http://www.morning.srnth.cn.gov.cn.srnth.cn http://www.morning.cfcdr.cn.gov.cn.cfcdr.cn http://www.morning.hytr.cn.gov.cn.hytr.cn http://www.morning.fktlr.cn.gov.cn.fktlr.cn http://www.morning.hqrkq.cn.gov.cn.hqrkq.cn