当前位置:网站首页>Kotlin basic learning 13
Kotlin basic learning 13
2022-07-02 03:24:00 【Never stop learning】
Catalog
1.Kotlin The default implementation of the interface of the language
2.Kotlin Abstract class learning of language 、
3.Kotlin Language defined generic classes
4.Kotlin Language definition generic function learning
5.Kotlin Language definition generic transformation practice
6.Kotlin Language definition generic type constraint learning
7.Kotlin Defined by language vararg keyword ( Dynamic parameters )
1.Kotlin The default implementation of the interface of the language
interface IUSB2{
// 1. Interface var You cannot assign values to the members of the interface ( But there are other ways )
// 2. Any class Interface wait val Represents read-only , Is it possible to assign values dynamically later ( There is no other way )
val usbVersionInfo:String //USB Version related information
get() = (1..100).shuffled().last().toString()
// val Unwanted set
val usbInsertDevice:String //USB Inserted device information
get() = " Advanced device access USB"
// val Unwanted set
fun insertUSB() : String
}
// mouse USB Implementation class
class Mouse2 : IUSB2 {
override val usbVersionInfo: String
get() = super.usbVersionInfo
override val usbInsertDevice: String
get() = super.usbInsertDevice
override fun insertUSB() = "Mouse $usbVersionInfo, $usbInsertDevice"
}
// TODO 101.Kotlin The default implementation of the interface of the language
// 1. Be careful : It's not right to do so , Because interface members are originally used to declare Standards
// But it can be done when the interface member is declared , Complete the implementation of interface members
fun main(){
// val iusb1 : Mouse2()
// println(iusb1.insertUSB())
}2.Kotlin Abstract class learning of language 、
abstract class BaseActivity{
fun onCreate(){
setContentView(getLayoutID())
initView()
initData()
initXXX()
}
private fun setContentView(layoutID:Int) = println(" load ${layoutID} Layout xml in ")
abstract fun initView()
abstract fun initData()
abstract fun initXXX()
abstract fun getLayoutID() : Int
}
class MainActivity : BaseActivity(){
override fun initView() {
println(" Do specific initialization View The implementation of the ")
}
override fun initData() {
println(" Implement specific initialization data ")
}
override fun initXXX() {
println(" Do specific initialization XXX The implementation of the ")
}
override fun getLayoutID(): Int = 546
fun show(){
super.onCreate()
}
}
// TODO 101.Kotlin Abstract class learning of language
fun main() = MainActivity().show()3.Kotlin Language defined generic classes
class KtBase102<T>(private val obj : T){// Universal output
fun show () = println(" Universal output :$obj")
}
data class Student(val name :String , val age : Int,val sex : Char)
data class Teacher(val name :String , val age : Int,val sex : Char)
// TODO 102.Kotlin Language defined generic classes
fun main(){
val stu1 = Student(" Zhang San ",66,' male ')
val stu2 = Student(" Li Si ",56,' male ')
val tea1 = Teacher(" Wang Wu ",46,' male ')
val tea2 = Teacher(" Zhao Liu ",36,' male ')
KtBase102(stu1).show()
KtBase102(stu2).show()
KtBase102(tea1).show()
KtBase102(tea2).show()
KtBase102(String(" Liu er ".toByteArray())).show()
KtBase102(456).show()
KtBase102(456.23).show()
KtBase102(456.3f).show()
KtBase102(' male ').show()
}4.Kotlin Language definition generic function learning
// 1. Universal object returner Boolean To control whether to return Application takeif
class KtBase103<T>(private val isR : Boolean, private val obj : T){// Universal output
fun getObj() : T? = obj.takeIf { isR }
}
// TODO 103.Kotlin Language definition generic function learning
fun main(){
val stu1 = Student(" Zhang San ",66,' male ')
val stu2 = Student(" Li Si ",56,' male ')
val tea1 = Teacher(" Wang Wu ",46,' male ')
val tea2 = Teacher(" Zhao Liu ",36,' male ')
//2. Four objects print
println(KtBase103(true,stu1).getObj())
println(KtBase103(true,stu2).getObj())
println(KtBase103(true,tea1).getObj())
println(KtBase103(true,tea2).getObj())
println(KtBase103(false,tea2).getObj() ?: " Your universal returner , Is to return null")
println()
//3. Object printing + run +? :
val r :Any = KtBase103(true,stu1).getObj()?.run {
// If getObj Return value Will come in
//this == getobj In itself
println(" The return object is :$this")
456.5f
} ?: println(" The universal returner returns null")
println(r)
println()
//4. Object printing + apply + ?:
val r3 : Teacher = KtBase103(true,tea1).getObj().apply {
if(this == null){
println(" Universal output returns null")
} else{
println(" The universal output object is :$this")
}
}!!
println("r3:$r3")
println()
show("Bxb")
show("Wuwu")
}
//5.show(t:T) + also + ?:
fun <B> show(item:B){
item ?.also {
println(" The universal object is :$it")
} ?: println(" eldest brother , Your universal object returner returns null")
}5.Kotlin Language definition generic transformation practice
package com.bxb.s6
// 1. class isMap map takeif map What type is it
class KtBase105<T>(val isMap:Boolean = false,val inputType :T){
inline fun <R> map(mapAction : (T) -> R) = mapAction(inputType).takeIf { isMap }
}
inline fun <I,O> map(inputValue: I,isMap: Boolean = true,mapActionLambda:(I) -> O)=
if(isMap) mapActionLambda(inputValue) else null
// TODO 104.Kotlin Language definition generic transformation practice
fun main(){
//2.map int -> str What type of
val p1 = KtBase105(isMap = false,inputType = 4563)
val r = p1.map {
it
it.toString()
" my it yes $it"
}
//4. Verify whether it is this type and null
val str1 : String = "OK1"
val str2 : String? = "OK2"
println(r is String)
println(r is String?)
println()
//3.map per -> stu What type of final reception
val p2 = KtBase105(true,Persons(" Li Si ",88))
val r2 : Students? = p2.map {
it
Students(it.name,it.age)
}
println(r2)
println()
//map function imitation RxJava Change operation
val r3 = map(123){
it.toString()
"map The parcel [$it]"
}
println(r3)
}
data class Persons(val name:String,val age : Int)
data class Students(val name:String,val age: Int)
6.Kotlin Language definition generic type constraint learning
package com.bxb.s6
open class MyAntClass(name : String) // Ancestry Top level parent class
open class PersonClass(name :String) : MyAntClass(name = name ) // Parent class
class StudentClass(name :String) : PersonClass(name = name ) // Subclass
class TeacherClass(name :String) : PersonClass(name = name ) // Subclass
class DogClass(name : String) // Other categories alternative
class CatClass(name : String)
// TODO 105.Kotlin Language definition generic type constraint learning
class KtBase106<T : PersonClass>(private val inputTypeValue:T,private val isR : Boolean = true){
// Universal object returner
fun getObj() = inputTypeValue.takeIf { isR }
}
fun main(){
val any = MyAntClass("Bxb1")
val per = PersonClass("Bxb1")
val stu = StudentClass("Bxb1")
val tea = TeacherClass("Bxb1")
val dog = DogClass("Wang1")
val r2 = KtBase106(per).getObj()
println(r2)
val r3 = KtBase106(stu).getObj()
println(r3)
val r4 = KtBase106(tea).getObj()
println(r4)
}7.Kotlin Defined by language vararg keyword ( Dynamic parameters )
package com.bxb.s6
class KtBase107<T>(vararg objects : T,var isMap : Boolean){
//1.objectArray:Array<T>
//out our T Can only be Read , Do not modify
private val objectArray : Array<out T> = objects
fun showObj(index : Int) : T? = objectArray[index].takeIf { isMap } ?: null
fun <O> mapObj(index: Int,mapAction : (T ?) -> O) = mapAction(objectArray[index].takeIf { isMap })
}
// TODO 107.Kotlin Defined by language vararg keyword ( Dynamic parameters )
fun main(){
val p:KtBase107<Any?> = KtBase107("Bxb",false,4585,4526.3f,456.32,null,'C',isMap = true)
println(p.showObj(0))
println(p.showObj(1))
println(p.showObj(2))
println(p.showObj(3))
println(p.showObj(4))
println(p.showObj(5))
println(p.showObj(6))
println()
val r: Int = p.mapObj(0){
it
it.toString()
it.toString().length
}
println(" The string length of the zeroth element is :$r")
val r2 : String = p.mapObj(2){
" My third element is :$it"
}
println(r2)
val p2:KtBase107<String> = KtBase107("AAA","BBB","CCC",isMap = true)
val r3 = p2.mapObj(2){
it
" I want to turn you into String type it:$it"
}
println(r3)
}
边栏推荐
- venn圖取交集
- 命名块 verilog
- /silicosis/geo/GSE184854_scRNA-seq_mouse_lung_ccr2/GSE184854_RAW/GSM5598265_matrix_inflection_demult
- What is hybrid web containers for SAP ui5
- Review materials of project management PMP high frequency examination sites (8-1)
- Mathematical calculation in real mode addressing
- Knowing things by learning | self supervised learning helps improve the effect of content risk control
- Verilog 过程赋值 区别 详解
- 浅谈线程池相关配置
- Redis set command line operation (intersection, union and difference, random reading, etc.)
猜你喜欢

Pointer array & array pointer

Verilog 避免 Latch

Review materials of project management PMP high frequency examination sites (8-1)

West digital decided to raise the price of flash memory products immediately after the factory was polluted by materials

Just a few simple steps - start playing wechat applet

Halcon image rectification

Uniapp uses canvas to generate posters and save them locally

Retrofit's callback hell is really vulnerable in kotlin synergy mode

创业了...

Design details of SAP e-commerce cloud footernavigationcomponent
随机推荐
Design details of SAP e-commerce cloud footernavigationcomponent
In the era of programmers' introspection, five-year-old programmers are afraid to go out for interviews
What is hybrid web containers for SAP ui5
Verilog wire type
Possible causes of runtime error
What is the binding path of SAP ui5
spark调优
Kotlin基础学习 15
Gradle foundation | customize the plug-in and upload it to jitpack
Screenshot literacy tool download and use
V-model of custom component
只需简单几步 - 开始玩耍微信小程序
[database]jdbc
Aaaaaaaaaaaa
Qualcomm platform WiFi -- P2P issue (2)
Cache processing scheme in high concurrency scenario
Yan Rong looks at how to formulate a multi cloud strategy in the era of hybrid cloud
Verilog state machine
流线线使用阻塞还是非阻塞
Global and Chinese markets for electronic laryngoscope systems 2022-2028: Research Report on technology, participants, trends, market size and share