当前位置:网站首页>Kotlin 基础学习13
Kotlin 基础学习13
2022-07-02 03:20:00 【学习不停息】
目录
1.Kotlin 语言的接口的默认实现
interface IUSB2{
// 1.接口var 也是不能给接口的成员赋值的(但是有其他方法)
// 2.任何类 接口 等等 val代表只读的,是不可以在后面动态赋值(也没有其他办法)
val usbVersionInfo:String //USB版本相关信息
get() = (1..100).shuffled().last().toString()
// val 不需要set
val usbInsertDevice:String //USB插入的设备信息
get() = "高级设备接入USB"
// val不需要set
fun insertUSB() : String
}
//鼠标USB实现类
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 语言的接口的默认实现
// 1.注意:这样做是不对的,因为接口成员本来就是用来声明标准的
// 但是可以在接口成员声明时,完成对接口成员的实现
fun main(){
// val iusb1 : Mouse2()
// println(iusb1.insertUSB())
}
2.Kotlin 语言的抽象类学习、
abstract class BaseActivity{
fun onCreate(){
setContentView(getLayoutID())
initView()
initData()
initXXX()
}
private fun setContentView(layoutID:Int) = println("加载${layoutID}布局xml中")
abstract fun initView()
abstract fun initData()
abstract fun initXXX()
abstract fun getLayoutID() : Int
}
class MainActivity : BaseActivity(){
override fun initView() {
println("做具体初始化View的实现")
}
override fun initData() {
println("做具体初始化数据的实现")
}
override fun initXXX() {
println("做具体初始化XXX的实现")
}
override fun getLayoutID(): Int = 546
fun show(){
super.onCreate()
}
}
// TODO 101.Kotlin 语言的抽象类学习
fun main() = MainActivity().show()
3.Kotlin 语言定义泛型类
class KtBase102<T>(private val obj : T){//万能输出器
fun show () = println("万能输出器:$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 语言定义泛型类
fun main(){
val stu1 = Student("张三",66,'男')
val stu2 = Student("李四",56,'男')
val tea1 = Teacher("王五",46,'男')
val tea2 = Teacher("赵六",36,'男')
KtBase102(stu1).show()
KtBase102(stu2).show()
KtBase102(tea1).show()
KtBase102(tea2).show()
KtBase102(String("刘二".toByteArray())).show()
KtBase102(456).show()
KtBase102(456.23).show()
KtBase102(456.3f).show()
KtBase102('男').show()
}
4.Kotlin 语言定义泛型函数学习
// 1.万能对象返回器 Boolean 来控制是否返回 运用takeif
class KtBase103<T>(private val isR : Boolean, private val obj : T){//万能输出器
fun getObj() : T? = obj.takeIf { isR }
}
// TODO 103.Kotlin 语言定义泛型函数学习
fun main(){
val stu1 = Student("张三",66,'男')
val stu2 = Student("李四",56,'男')
val tea1 = Teacher("王五",46,'男')
val tea2 = Teacher("赵六",36,'男')
//2.四个对象打印
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() ?: "你的万能返回器,是返回null")
println()
//3.对象打印 + run +? :
val r :Any = KtBase103(true,stu1).getObj()?.run {
//如果getObj 返回有值 就会进来
//this == getobj本身
println("返回对象是:$this")
456.5f
} ?: println("万能返回器返回null")
println(r)
println()
//4.对象打印 + apply + ?:
val r3 : Teacher = KtBase103(true,tea1).getObj().apply {
if(this == null){
println("万能输出器返回null")
} else{
println("万能输出器对象是:$this")
}
}!!
println("r3:$r3")
println()
show("Bxb")
show("Wuwu")
}
//5.show(t:T) + also + ?:
fun <B> show(item:B){
item ?.also {
println("万能对象是:$it")
} ?: println("大哥,你的万能对象返回器返回null")
}
5.Kotlin 语言定义泛型变换实战
package com.bxb.s6
// 1.类 isMap map takeif map 是什么类型
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 语言定义泛型变换实战
fun main(){
//2.map int -> str最终接受什么类型
val p1 = KtBase105(isMap = false,inputType = 4563)
val r = p1.map {
it
it.toString()
"我的it是$it"
}
//4.验证是否是此类型 和 null
val str1 : String = "OK1"
val str2 : String? = "OK2"
println(r is String)
println(r is String?)
println()
//3.map per -> stu 最终接收什么类型
val p2 = KtBase105(true,Persons("李四",88))
val r2 : Students? = p2.map {
it
Students(it.name,it.age)
}
println(r2)
println()
//map函数 模仿RxJava变换操作
val r3 = map(123){
it.toString()
"map包裹[$it]"
}
println(r3)
}
data class Persons(val name:String,val age : Int)
data class Students(val name:String,val age: Int)
6.Kotlin 语言定义泛型类型约束学习
package com.bxb.s6
open class MyAntClass(name : String) //祖宗类 顶级父类
open class PersonClass(name :String) : MyAntClass(name = name ) // 父类
class StudentClass(name :String) : PersonClass(name = name ) // 子类
class TeacherClass(name :String) : PersonClass(name = name ) // 子类
class DogClass(name : String) //其他类 另类
class CatClass(name : String)
// TODO 105.Kotlin 语言定义泛型类型约束学习
class KtBase106<T : PersonClass>(private val inputTypeValue:T,private val isR : Boolean = true){
//万能对象返回器
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 语言定义的vararg关键字(动态参数)
package com.bxb.s6
class KtBase107<T>(vararg objects : T,var isMap : Boolean){
//1.objectArray:Array<T>
//out 我们的T只能被 读取,不能修改
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 语言定义的vararg关键字(动态参数)
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("第零个元素的字符串长度是:$r")
val r2 : String = p.mapObj(2){
"我的第三个元素是:$it"
}
println(r2)
val p2:KtBase107<String> = KtBase107("AAA","BBB","CCC",isMap = true)
val r3 = p2.mapObj(2){
it
"我要把你变成String类型 it:$it"
}
println(r3)
}
边栏推荐
- Qualcomm platform WiFi -- Native crash caused by WiFi
- 知物由学 | 自监督学习助力内容风控效果提升
- Baohong industry | what misunderstandings should we pay attention to when diversifying investment
- Global and Chinese market of X-ray detectors 2022-2028: Research Report on technology, participants, trends, market size and share
- ORA-01547、ORA-01194、ORA-01110
- GSE104154_ scRNA-seq_ fibrotic MC_ bleomycin/normalized AM3
- Gradle notes
- Redis set command line operation (intersection, union and difference, random reading, etc.)
- Rotating frame target detection mmrotate v0.3.1 learning model
- Aaaaaaaaaaaa
猜你喜欢
2022-2028 global aluminum beverage can coating industry research and trend analysis report
JIT deep analysis
Render header usage of El table
[JS reverse series] analysis of a customs publicity platform
MySQL connection query and subquery
【JVM】创建对象的流程详解
verilog 并行块实现
图扑软件通过 CMMI5 级认证!| 国际软件领域高权威高等级认证
Use usedeferredvalue for asynchronous rendering
West digital decided to raise the price of flash memory products immediately after the factory was polluted by materials
随机推荐
aaaaaaaaaaaaa
uniapp 使用canvas 生成海报并保存到本地
Aaaaaaaaaaaa
V-model of custom component
Continuous assignment of Verilog procedure
2022-2028 global nano abrasive industry research and trend analysis report
Find duplicates [Abstract binary / fast and slow pointer / binary enumeration]
The capacity is upgraded again, and the new 256gb large capacity specification of Lexar rexa 2000x memory card is added
SAML2.0 notes (I)
JS <2>
焱融看 | 混合云时代下,如何制定多云策略
流线线使用阻塞还是非阻塞
4. Find the median of two positive arrays
C # joint Halcon's experience of breaking away from Halcon environment and various error reporting solutions
Verilog state machine
buu_ re_ crackMe
Force deduction daily question 540 A single element in an ordered array
2022-2028 global human internal visualization system industry research and trend analysis report
Delphi xe10.4 installing alphacontrols15.12
GB/T-2423. XX environmental test documents, including the latest documents