当前位置:网站首页>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)
}
边栏推荐
猜你喜欢
Find duplicates [Abstract binary / fast and slow pointer / binary enumeration]
表单自定义校验规则
[HCIA continuous update] overview of dynamic routing protocol
Form custom verification rules
Kubernetes cluster storageclass persistent storage resource core concept and use
Review materials of project management PMP high frequency examination sites (8-1)
MySQL advanced (Advanced) SQL statement (II)
SAML2.0 笔记(一)
数据传输中的成帧
West digital decided to raise the price of flash memory products immediately after the factory was polluted by materials
随机推荐
Kotlin基础学习 15
Docker安装canal、mysql进行简单测试与实现redis和mysql缓存一致性
Global and Chinese market of gynaecological health training manikin 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese market of autotransfusion bags 2022-2028: Research Report on technology, participants, trends, market size and share
3124. Word list
Global and Chinese market of handheld ultrasonic scanners 2022-2028: Research Report on technology, participants, trends, market size and share
Tupu software has passed CMMI5 certification| High authority and high-level certification in the international software field
Load different fonts in QML
GSE104154_scRNA-seq_fibrotic MC_bleomycin/normalized AM3
V-model of custom component
Exchange rate query interface
[数据库]JDBC
在QML中加载不同字体
Apple added the first iPad with lightning interface to the list of retro products
aaaaaaaaaaaaa
One of the future trends of SAP ui5: embrace typescript
[golang] leetcode intermediate bracket generation & Full Permutation
Global and Chinese markets for hand hygiene monitoring systems 2022-2028: Research Report on technology, participants, trends, market size and share
ZABBIX API creates hosts in batches according to the host information in Excel files
js生成随机数