当前位置:网站首页>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")
}边栏推荐
- Review materials of project management PMP high frequency examination sites (8-1)
- [HCIA continuous update] working principle of OSPF Protocol
- Aaaaaaaaaaaa
- 《MATLAB 神经网络43个案例分析》:第41章 定制神经网络的实现——神经网络的个性化建模与仿真
- Go execute shell command
- Cache processing scheme in high concurrency scenario
- Apple added the first iPad with lightning interface to the list of retro products
- 流线线使用阻塞还是非阻塞
- js生成随机数
- Merge interval, linked list, array
猜你喜欢

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

Gradle foundation | customize the plug-in and upload it to jitpack
![[HCIA continuous update] working principle of OSPF Protocol](/img/bc/4eeb091c511fd563fb1e00c8c8881a.jpg)
[HCIA continuous update] working principle of OSPF Protocol

Docker安装canal、mysql进行简单测试与实现redis和mysql缓存一致性

Yan Rong looks at how to formulate a multi cloud strategy in the era of hybrid cloud

Verilog 过程连续赋值

MSI announced that its motherboard products will cancel all paper accessories

Xiaomi, a young engineer, was just going to make soy sauce

命名块 verilog

Detailed explanation of ThreadLocal
随机推荐
C # joint Halcon's experience of breaking away from Halcon environment and various error reporting solutions
[数据库]JDBC
Kotlin基础学习 15
In depth analysis of C language - variable error prone knowledge points # dry goods inventory #
Go执行shell命令
ThreadLocal详解
venn圖取交集
[HCIA continuous update] working principle of OSPF Protocol
Verilog avoid latch
Mmsegmentation series training and reasoning their own data set (3)
Qualcomm platform wifi-- WPA_ supplicant issue
Knowing things by learning | self supervised learning helps improve the effect of content risk control
JIT deep analysis
Verilog 过程赋值 区别 详解
Apple added the first iPad with lightning interface to the list of retro products
Mathematical calculation in real mode addressing
只需简单几步 - 开始玩耍微信小程序
Delphi xe10.4 installing alphacontrols15.12
Grpc快速实践
Verilog 避免 Latch