当前位置:网站首页>Kotlin基础学习 14
Kotlin基础学习 14
2022-07-02 03:20:00 【学习不停息】
目录
1.Kotlin语言的[]操作符学习
class KtBase108<INPUT>(vararg objects : INPUT, val isR:Boolean= true){
//开启input泛型的只读模式
private val objectArray:Array<out INPUT> = objects
//5种返回类型变换的解释
fun getR1() : Array<out INPUT> ? = objectArray.takeIf { isR }
fun getR2() : Any = objectArray.takeIf { isR } ?: "你是null"
fun getR3() : Any ? = objectArray.takeIf { isR } ?: "你是null" ?: null
fun getR4(index : Int) : INPUT ? = objectArray[index].takeIf { isR } ?: null
fun getR5(index : Int) : Any ? = objectArray[index].takeIf { isR } ?: "AAA" ?: 456 ?: 4856.63f ?:'C' ?: false ?: null
operator fun get(index: Int) : INPUT ?= objectArray[index].takeIf { isR }
}
fun <INPUT> inputObj(item : INPUT){
println((item as String?)?.length ?: "传递的泛型数据是null")
}
// TODO 108.Kotlin语言的[]操作符学习
fun main(){
inputObj("Bxb")
inputObj("Wuwu")
inputObj(null)
println()
val p1 : KtBase108<String?> = KtBase108("张三","李四","王五",null)
var r : String?=p1[0]
val r2 :String? = p1[3]
println(r)
println(p1[1])
println(p1[2])
println(r2)
}2.Kotlin语言的out -协变学习
//生产者 out T 协变
interface Producer<out T>{
//out T 代表整个生产者里面 这个T 只能被读取,不能被修改
fun producer() : T
}
//消费者 in T 逆变
interface Consumer<in T>{
// 只能被修改
fun consumer(item : T)
}
//生产者&消费者
interface ProcuderAndConsumer<T>{
fun consumer(item : T)
fun procuder() : T
}
open class Animal
open class Humanity : Animal()
open class Man : Humanity()
open class Woman : Humanity()
class ProcuderClass1 : Producer<Animal>{
override fun producer(): Animal {
println("生产者 Animal")
return Animal()
}
}
class ProcuderClass2 : Producer<Humanity>{
override fun producer(): Humanity {
println("生产者 Humanity")
return Humanity()
}
}
class ProcuderClass3 : Producer<Man>{
override fun producer(): Man {
println("生产者 Man")
return Man()
}
}
class ProcuderClass4 : Producer<Woman>{
override fun producer(): Woman {
println("生产者 Woman")
return Woman()
}
}
// TODO 109.Kotlin语言的out -协变学习
fun main(){
val p1 : Producer<Animal> = ProcuderClass1() //ProcuderClass1 本来就是传递 Animal,当然可以
val p2 : Producer<Animal> = ProcuderClass2() //ProcuderClass2 本来就是传递 Humanity,居然也可以,因为out
val p3 : Producer<Animal> = ProcuderClass3() //ProcuderClass3 本来就是传递 Man,居然也可以,因为out
val p4 : Producer<Animal> = ProcuderClass4() //ProcuderClass4 本来就是传递 Woman,居然也可以,因为out
//out:泛型的子类对象 可以赋值给 泛型的父类对象
//out: 泛型具体的子类对象 可以赋值给 泛型声明处的父类对象
}3.Kotlin语言的in -逆变学习
class ConsumerClass1 : Consumer<Animal>{
override fun consumer(item : Animal) {
println("消费者 Animal")
}
}
class ConsumerClass2 : Consumer<Humanity>{
override fun consumer(item : Humanity) {
println("消费者 Humanity")
}
}
class ConsumerClass3 : Consumer<Man>{
override fun consumer(item : Man) {
println("消费者 Man")
}
}
class ConsumerClass4 : Consumer<Woman>{
override fun consumer(item : Woman) {
println("消费者 Woman")
}
}
// TODO 110.Kotlin语言的in -逆变学习
fun main(){
val p1 : Consumer<Man> = ConsumerClass1() //ConsumerClass1 本来就是传递 Animal,当然可以
val p2 : Consumer<Woman> = ConsumerClass2() //ConsumerClass2 本来就是传递 Humanity,居然也可以,因为in
//默认情况下:泛型具体处的父类 是不可以赋值给 泛型声明处的子类的
//in : 泛型具体处的父类 是可以赋值给 泛型声明处的子类的
}
4.Kotlin语言中使用in out
class SetClass<in T>(){
//函数对T只能修改,不能读取
fun set1(item:T){
println("set1 你要设置的item是:$item")
}
fun set2(item:T){
println("set2 你要设置的item是:$item")
}
fun set3(item:T){
println("set3 你要设置的item是:$item")
}
}
class GetClass<out T>(_item : T){
val item : T = _item
//函数对T只能读取,不能修改
fun get1() :T{
return item
}
fun get2() :T{
return item
}
fun get3() :T{
return item
}
}
// TODO 115.Kotlin语言中使用in out
fun main(){
//逆变 in 只能修改,不能读取
val p1 = SetClass<String>()
p1.set1("Bxb")
p1.set2("Bob")
println()
//协变 out 只能读取,不能修改
val p2 = GetClass("张三")
println(p2.get1())
val p3 = GetClass("李四")
println(p3.get2())
}5.Kotlin语言的reified关键字学习
data class ObjectClass1(val name : String,val age:Int,val study :String)
data class ObjectClass2(val name : String,val age:Int,val study :String)
data class ObjectClass3(val name : String,val age:Int,val study :String)
class KtBase112 {
//默认随机输出一个对象,如果此对象和用户指定的对象不一致,我们就启用备用对象,否则直接返回对象
inline fun<reified T> randomOrDefault(defaultLambdaAction:()->T):T?{
val objList:List<Any> = listOf(ObjectClass1("obj1 张三",22,"学习C++"),
ObjectClass2("obj2 李四",23,"学习Java"),
ObjectClass3("obj3 王五",24,"学习Python"))
val randomObj : Any?= objList.shuffled().first()
println("您随机产生的对象幸运儿是:$randomObj")
return randomObj.takeIf { it is T } as T?
?: defaultLambdaAction()
}
}
// TODO 116.Kotlin语言的reified关键字学习
fun main(){
val finalResult = KtBase112().randomOrDefault <ObjectClass1>{
println("由于随机产生的对象和我们指定的对象不一致,所以我们采用备用对象")
ObjectClass1("备用 obj1 张三",22,"学习C++")
}
println("客户端最终结果:$finalResult")
}边栏推荐
- Gradle notes
- C reflection practice
- Qualcomm platform WiFi -- Native crash caused by WiFi
- 2022-2028 global deep sea generator controller industry research and trend analysis report
- Cache processing scheme in high concurrency scenario
- GB/T-2423.xx 环境试验文件,整理包括了最新的文件里面
- verilog 并行块实现
- Intersection of Venn graph
- Global and Chinese markets for electronic laryngoscope systems 2022-2028: Research Report on technology, participants, trends, market size and share
- Verilog reg register, vector, integer, real, time register
猜你喜欢

ThreadLocal详解

Baohong industry | 6 financial management models at different stages of life

Continuous assignment of Verilog procedure

Named block Verilog

Force deduction daily question 540 A single element in an ordered array

uniapp 使用canvas 生成海报并保存到本地

2022-2028 global human internal visualization system industry research and trend analysis report

MSI announced that its motherboard products will cancel all paper accessories

Use usedeferredvalue for asynchronous rendering

2022-2028 global manual dental cleaning equipment industry research and trend analysis report
随机推荐
SAML2.0 笔记(一)
ThreadLocal详解
Tupu software has passed CMMI5 certification| High authority and high-level certification in the international software field
verilog 并行块实现
Continuous assignment of Verilog procedure
Principle of computer composition - interview questions for postgraduate entrance examination (review outline, key points and reference)
焱融看 | 混合云时代下,如何制定多云策略
Mathematical calculation in real mode addressing
2022-2028 global military computer industry research and trend analysis report
venn圖取交集
Redis set command line operation (intersection, union and difference, random reading, etc.)
Verilog 时序控制
spark调优
Global and Chinese market of X-ray detectors 2022-2028: Research Report on technology, participants, trends, market size and share
OSPF LSA message parsing (under update)
West digital decided to raise the price of flash memory products immediately after the factory was polluted by materials
Verilog avoid latch
2022-2028 global nano abrasive industry research and trend analysis report
图扑软件通过 CMMI5 级认证!| 国际软件领域高权威高等级认证
创业了...