当前位置:网站首页>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 soft capsule manufacturing machine industry research and trend analysis report
- buu_ re_ crackMe
- Form custom verification rules
- V-model of custom component
- 初出茅庐市值1亿美金的监控产品Sentry体验与架构
- Global and Chinese market of gynaecological health training manikin 2022-2028: Research Report on technology, participants, trends, market size and share
- Possible causes of runtime error
- Global and Chinese markets for hand hygiene monitoring systems 2022-2028: Research Report on technology, participants, trends, market size and share
- [C Advanced] brother Peng takes you to play with strings and memory functions
- 2022-2028 global military computer industry research and trend analysis report
猜你喜欢

Common means of modeling: aggregation

Download and use of the super perfect screenshot tool snipaste

How to develop digital collections? How to develop your own digital collections
![[HCIA continuous update] working principle of OSPF Protocol](/img/bc/4eeb091c511fd563fb1e00c8c8881a.jpg)
[HCIA continuous update] working principle of OSPF Protocol
![[golang] leetcode intermediate bracket generation & Full Permutation](/img/93/ca38d97c721ccba2505052ef917788.jpg)
[golang] leetcode intermediate bracket generation & Full Permutation

How to establish its own NFT market platform in 2022

Mmsegmentation series training and reasoning their own data set (3)

Sentry experience and architecture, a fledgling monitoring product with a market value of $100million

One of the future trends of SAP ui5: embrace typescript

2022-2028 global human internal visualization system industry research and trend analysis report
随机推荐
Verilog avoid latch
Verilog 过程赋值 区别 详解
Qualcomm platform WiFi -- P2P issue (2)
venn圖取交集
[golang] leetcode intermediate bracket generation & Full Permutation
tarjan2
2022 hoisting machinery command examination paper and summary of hoisting machinery command examination
小米青年工程师,本来只是去打个酱油
Go execute shell command
3048. Number of words
C#聯合halcon脫離halcon環境以及各種報錯解决經曆
Qualcomm platform wifi-- WPA_ supplicant issue
表单自定义校验规则
2022-2028 global military computer industry research and trend analysis report
Pointer array & array pointer
Verilog 时序控制
终日乾乾,夕惕若厉
3124. Word list
What is hybrid web containers for SAP ui5
Framing in data transmission