当前位置:网站首页>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")
}
边栏推荐
- [HCIA continuous update] overview of dynamic routing protocol
- Uniapp uses canvas to generate posters and save them locally
- Named block Verilog
- Form custom verification rules
- Qualcomm platform WiFi -- P2P issue (2)
- Gradle foundation | customize the plug-in and upload it to jitpack
- PMP personal sprint preparation experience
- Calculation of page table size of level 2, level 3 and level 4 in protection mode (4k=4*2^10)
- Force deduction daily question 540 A single element in an ordered array
- Verilog state machine
猜你喜欢
halcon图像矫正
汇率的查询接口
SAML2.0 笔记(一)
Gradle foundation | customize the plug-in and upload it to jitpack
焱融看 | 混合雲時代下,如何制定多雲策略
[JS reverse series] analysis of a customs publicity platform
Verilog avoid latch
West digital decided to raise the price of flash memory products immediately after the factory was polluted by materials
Review materials of project management PMP high frequency examination sites (8-1)
[HCIA continuous update] overview of dynamic routing protocol
随机推荐
终日乾乾,夕惕若厉
Gradle 笔记
Global and Chinese markets for infant care equipment, 2022-2028: Research Report on technology, participants, trends, market size and share
In depth interpretation of pytest official documents (26) customized pytest assertion error information
Verilog 避免 Latch
Custom classloader that breaks parental delegation
2022 hoisting machinery command examination paper and summary of hoisting machinery command examination
Go执行shell命令
JDBC details
SAML2.0 笔记(一)
What is hybrid web containers for SAP ui5
KL divergence is a valuable article
表单自定义校验规则
One of the future trends of SAP ui5: embrace typescript
Discrimination between sap Hana, s/4hana and SAP BTP
Just a few simple steps - start playing wechat applet
Verilog state machine
Principle of computer composition - interview questions for postgraduate entrance examination (review outline, key points and reference)
Kubernetes cluster storageclass persistent storage resource core concept and use
Use blocking or non blocking for streamline