当前位置:网站首页>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")
}边栏推荐
- 2022-2028 global manual dental cleaning equipment industry research and trend analysis report
- Continuous assignment of Verilog procedure
- Intersection vengraph
- ZABBIX API creates hosts in batches according to the host information in Excel files
- Verilog 过程赋值 区别 详解
- Xiaomi, a young engineer, was just going to make soy sauce
- Global and Chinese markets for hand hygiene monitoring systems 2022-2028: Research Report on technology, participants, trends, market size and share
- Qualcomm platform wifi-- WPA_ supplicant issue
- Global and Chinese markets for welding equipment and consumables 2022-2028: Research Report on technology, participants, trends, market size and share
- [HCIA continuous update] working principle of OSPF Protocol
猜你喜欢

Retrofit's callback hell is really vulnerable in kotlin synergy mode

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

OSPF LSA message parsing (under update)

Verilog 过程连续赋值

Screenshot literacy tool download and use

焱融看 | 混合云时代下,如何制定多云策略

ThreadLocal详解

只需简单几步 - 开始玩耍微信小程序

How to establish its own NFT market platform in 2022

Uniapp uses canvas to generate posters and save them locally
随机推荐
verilog REG 寄存器、向量、整数、实数、时间寄存器
Redis cluster
Uniapp uses canvas to generate posters and save them locally
KL divergence is a valuable article
自定义组件的 v-model
2022-2028 global aluminum beverage can coating industry research and trend analysis report
West digital decided to raise the price of flash memory products immediately after the factory was polluted by materials
Generate random numbers that obey normal distribution
Discussion on related configuration of thread pool
Discrimination between sap Hana, s/4hana and SAP BTP
The capacity is upgraded again, and the new 256gb large capacity specification of Lexar rexa 2000x memory card is added
ZABBIX API creates hosts in batches according to the host information in Excel files
Verilog 时序控制
GB/T-2423. XX environmental test documents, including the latest documents
Xiaomi, a young engineer, was just going to make soy sauce
uniapp 使用canvas 生成海报并保存到本地
Verilog 线型wire 种类
3124. Word list
ORA-01547、ORA-01194、ORA-01110
In depth interpretation of pytest official documents (26) customized pytest assertion error information