当前位置:网站首页>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")
}边栏推荐
- A list of job levels and salaries in common Internet companies. Those who have conditions must enter big factories. The salary is really high
- 浅谈线程池相关配置
- Delphi xe10.4 installing alphacontrols15.12
- Use blocking or non blocking for streamline
- JS introduction < 1 >
- Gradle 笔记
- Tupu software has passed CMMI5 certification| High authority and high-level certification in the international software field
- Retrofit's callback hell is really vulnerable in kotlin synergy mode
- Common means of modeling: aggregation
- Global and Chinese market of bone adhesives 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢

verilog 并行块实现

Cache processing scheme in high concurrency scenario

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

Learn PWN from CTF wiki - ret2shellcode

Halcon image rectification

2022-2028 global encryption software industry research and trend analysis report

Verilog 避免 Latch

2022-2028 global manual dental cleaning equipment industry research and trend analysis report

Common means of modeling: aggregation

小米青年工程师,本来只是去打个酱油
随机推荐
Baohong industry | 6 financial management models at different stages of life
Use blocking or non blocking for streamline
GSE104154_ scRNA-seq_ fibrotic MC_ bleomycin/normalized AM3
[HCIA continuous update] working principle of OSPF Protocol
Baohong industry | four basic knowledge necessary for personal finance
Merge interval, linked list, array
JS introduction < 1 >
焱融看 | 混合云时代下,如何制定多云策略
C#聯合halcon脫離halcon環境以及各種報錯解决經曆
4. Find the median of two positive arrays
流线线使用阻塞还是非阻塞
< job search> process and signal
Verilog timing control
Principle of computer composition - interview questions for postgraduate entrance examination (review outline, key points and reference)
Qualcomm platform wifi-- WPA_ supplicant issue
Named block Verilog
verilog REG 寄存器、向量、整数、实数、时间寄存器
/silicosis/geo/GSE184854_scRNA-seq_mouse_lung_ccr2/GSE184854_RAW/GSM5598265_matrix_inflection_demult
Continuous assignment of Verilog procedure
[数据库]JDBC