当前位置:网站首页>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")
}边栏推荐
- 表单自定义校验规则
- Unit · elementary C # learning notes
- GSE104154_ scRNA-seq_ fibrotic MC_ bleomycin/normalized AM3
- Gradle notes
- Verilog avoid latch
- Mmsegmentation series training and reasoning their own data set (3)
- 自定义组件的 v-model
- Large screen visualization from bronze to the advanced king, you only need a "component reuse"!
- Global and Chinese market of gynaecological health training manikin 2022-2028: Research Report on technology, participants, trends, market size and share
- Redis set command line operation (intersection, union and difference, random reading, etc.)
猜你喜欢

Common means of modeling: aggregation

Verilog 避免 Latch

Use usedeferredvalue for asynchronous rendering

Xiaomi, a young engineer, was just going to make soy sauce
![Find duplicates [Abstract binary / fast and slow pointer / binary enumeration]](/img/9b/3c001c3b86ca3f8622daa7f7687cdb.png)
Find duplicates [Abstract binary / fast and slow pointer / binary enumeration]

JIT deep analysis

Discussion on related configuration of thread pool

C shallow copy and deep copy

How to establish its own NFT market platform in 2022

命名块 verilog
随机推荐
Verilog timing control
[JVM] detailed description of the process of creating objects
Force deduction daily question 540 A single element in an ordered array
Verilog state machine
Continuous assignment of Verilog procedure
What is hybrid web containers for SAP ui5
Spark Tuning
What is the binding path of SAP ui5
高并发场景下缓存处理方案
2022 hoisting machinery command examination paper and summary of hoisting machinery command examination
tarjan2
How to establish its own NFT market platform in 2022
Docker installs canal and MySQL for simple testing and implementation of redis and MySQL cache consistency
Discussion on related configuration of thread pool
Download and use of the super perfect screenshot tool snipaste
终日乾乾,夕惕若厉
寻找重复数[抽象二分/快慢指针/二进制枚举]
跟着CTF-wiki学pwn——ret2shellcode
/silicosis/geo/GSE184854_scRNA-seq_mouse_lung_ccr2/GSE184854_RAW/GSM5598265_matrix_inflection_demult
C#聯合halcon脫離halcon環境以及各種報錯解决經曆