当前位置:网站首页>Kotlin basic learning 14
Kotlin basic learning 14
2022-07-02 03:24:00 【Never stop learning】
Catalog
1.Kotlin Linguistic [] Operators learn
2.Kotlin Linguistic out - Covariant learning
3.Kotlin Linguistic in - Inverse learning
4.Kotlin Used in language in out
5.Kotlin Linguistic reified Keyword learning
1.Kotlin Linguistic [] Operators learn
class KtBase108<INPUT>(vararg objects : INPUT, val isR:Boolean= true){
// Turn on input Generic read-only mode
private val objectArray:Array<out INPUT> = objects
//5 An explanation of the return type transformation
fun getR1() : Array<out INPUT> ? = objectArray.takeIf { isR }
fun getR2() : Any = objectArray.takeIf { isR } ?: " You are a null"
fun getR3() : Any ? = objectArray.takeIf { isR } ?: " You are a 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 ?: " The generic data passed is null")
}
// TODO 108.Kotlin Linguistic [] Operators learn
fun main(){
inputObj("Bxb")
inputObj("Wuwu")
inputObj(null)
println()
val p1 : KtBase108<String?> = KtBase108(" Zhang San "," Li Si "," Wang Wu ",null)
var r : String?=p1[0]
val r2 :String? = p1[3]
println(r)
println(p1[1])
println(p1[2])
println(r2)
}2.Kotlin Linguistic out - Covariant learning
// producer out T Covariance
interface Producer<out T>{
//out T Represents the whole producer This T Can only be read , Cannot be modified
fun producer() : T
}
// consumer in T Inversion
interface Consumer<in T>{
// Can only be modified
fun consumer(item : T)
}
// producer & consumer
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(" producer Animal")
return Animal()
}
}
class ProcuderClass2 : Producer<Humanity>{
override fun producer(): Humanity {
println(" producer Humanity")
return Humanity()
}
}
class ProcuderClass3 : Producer<Man>{
override fun producer(): Man {
println(" producer Man")
return Man()
}
}
class ProcuderClass4 : Producer<Woman>{
override fun producer(): Woman {
println(" producer Woman")
return Woman()
}
}
// TODO 109.Kotlin Linguistic out - Covariant learning
fun main(){
val p1 : Producer<Animal> = ProcuderClass1() //ProcuderClass1 It's delivery Animal, Certainly.
val p2 : Producer<Animal> = ProcuderClass2() //ProcuderClass2 It's delivery Humanity, It's OK , because out
val p3 : Producer<Animal> = ProcuderClass3() //ProcuderClass3 It's delivery Man, It's OK , because out
val p4 : Producer<Animal> = ProcuderClass4() //ProcuderClass4 It's delivery Woman, It's OK , because out
//out: Subclass objects of generics I can assign a value to Generic parent object
//out: Generic concrete subclass objects I can assign a value to The parent object at the generic declaration
}3.Kotlin Linguistic in - Inverse learning
class ConsumerClass1 : Consumer<Animal>{
override fun consumer(item : Animal) {
println(" consumer Animal")
}
}
class ConsumerClass2 : Consumer<Humanity>{
override fun consumer(item : Humanity) {
println(" consumer Humanity")
}
}
class ConsumerClass3 : Consumer<Man>{
override fun consumer(item : Man) {
println(" consumer Man")
}
}
class ConsumerClass4 : Consumer<Woman>{
override fun consumer(item : Woman) {
println(" consumer Woman")
}
}
// TODO 110.Kotlin Linguistic in - Inverse learning
fun main(){
val p1 : Consumer<Man> = ConsumerClass1() //ConsumerClass1 It's delivery Animal, Certainly.
val p2 : Consumer<Woman> = ConsumerClass2() //ConsumerClass2 It's delivery Humanity, It's OK , because in
// By default : The parent class of the generic concrete Cannot be assigned to Of subclasses at the generic declaration
//in : The parent class of the generic concrete It can be assigned to Of subclasses at the generic declaration
}
4.Kotlin Used in language in out
class SetClass<in T>(){
// Function pair T It can only be modified , Cannot read
fun set1(item:T){
println("set1 What you want to set item yes :$item")
}
fun set2(item:T){
println("set2 What you want to set item yes :$item")
}
fun set3(item:T){
println("set3 What you want to set item yes :$item")
}
}
class GetClass<out T>(_item : T){
val item : T = _item
// Function pair T Can only read , Do not modify
fun get1() :T{
return item
}
fun get2() :T{
return item
}
fun get3() :T{
return item
}
}
// TODO 115.Kotlin Used in language in out
fun main(){
// Inversion in It can only be modified , Cannot read
val p1 = SetClass<String>()
p1.set1("Bxb")
p1.set2("Bob")
println()
// Covariance out Can only read , Do not modify
val p2 = GetClass(" Zhang San ")
println(p2.get1())
val p3 = GetClass(" Li Si ")
println(p3.get2())
}5.Kotlin Linguistic reified Keyword learning
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 {
// By default, an object is output randomly , If this object is inconsistent with the object specified by the user , We will enable the standby object , Otherwise, the object is returned directly
inline fun<reified T> randomOrDefault(defaultLambdaAction:()->T):T?{
val objList:List<Any> = listOf(ObjectClass1("obj1 Zhang San ",22," Study C++"),
ObjectClass2("obj2 Li Si ",23," Study Java"),
ObjectClass3("obj3 Wang Wu ",24," Study Python"))
val randomObj : Any?= objList.shuffled().first()
println(" The lucky ones you randomly generate are :$randomObj")
return randomObj.takeIf { it is T } as T?
?: defaultLambdaAction()
}
}
// TODO 116.Kotlin Linguistic reified Keyword learning
fun main(){
val finalResult = KtBase112().randomOrDefault <ObjectClass1>{
println(" Because the randomly generated object is inconsistent with the object we specify , So we use alternate objects ")
ObjectClass1(" spare obj1 Zhang San ",22," Study C++")
}
println(" Client end result :$finalResult")
}边栏推荐
- h5中的页面显示隐藏执行事件
- verilog REG 寄存器、向量、整数、实数、时间寄存器
- Detailed explanation of ThreadLocal
- Merge interval, linked list, array
- Docker installs canal and MySQL for simple testing and implementation of redis and MySQL cache consistency
- tarjan2
- Spark Tuning
- GSE104154_scRNA-seq_fibrotic MC_bleomycin/normalized AM3
- FFMpeg AVFrame 的概念.
- C # joint Halcon's experience of breaking away from Halcon environment and various error reporting solutions
猜你喜欢

Verilog state machine

《MATLAB 神經網絡43個案例分析》:第42章 並行運算與神經網絡——基於CPU/GPU的並行神經網絡運算

Verilog parallel block implementation
![Find duplicates [Abstract binary / fast and slow pointer / binary enumeration]](/img/9b/3c001c3b86ca3f8622daa7f7687cdb.png)
Find duplicates [Abstract binary / fast and slow pointer / binary enumeration]

Large screen visualization from bronze to the advanced king, you only need a "component reuse"!

Docker installs canal and MySQL for simple testing and implementation of redis and MySQL cache consistency

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

halcon图像矫正

Download and use of the super perfect screenshot tool snipaste

JS <2>
随机推荐
Design details of SAP e-commerce cloud footernavigationcomponent
Global and Chinese markets for ultrasonic probe disinfection systems 2022-2028: Research Report on technology, participants, trends, market size and share
数据传输中的成帧
Intersection vengraph
2022 hoisting machinery command examination paper and summary of hoisting machinery command examination
Find duplicates [Abstract binary / fast and slow pointer / binary enumeration]
Discrimination between sap Hana, s/4hana and SAP BTP
《MATLAB 神经网络43个案例分析》:第42章 并行运算与神经网络——基于CPU/GPU的并行神经网络运算
Kubernetes cluster storageclass persistent storage resource core concept and use
Form custom verification rules
One of the future trends of SAP ui5: embrace typescript
[HCIA continuous update] overview of dynamic routing protocol
C # joint Halcon's experience of breaking away from Halcon environment and various error reporting solutions
Comment élaborer une stratégie nuageuse à l'ère des nuages mixtes
表单自定义校验规则
《MATLAB 神经网络43个案例分析》:第41章 定制神经网络的实现——神经网络的个性化建模与仿真
Continuous assignment of Verilog procedure
GSE104154_ scRNA-seq_ fibrotic MC_ bleomycin/normalized AM3
MySQL connection query and subquery
Gradle foundation | customize the plug-in and upload it to jitpack