kotlin 泛型
2022-11-20 本文已影响0人
Bfmall
一:泛型用法
/**
* DESC : 泛型
*/
const val KtBaseGenericityTest01_TAG = "KtBaseGenericityTest01"
class ReadInfo<T>(private val obj: T) {
fun showInfo() {
Log.d(KtBaseGenericityTest01_TAG, "类的信息:$obj")
}
}
data class Student(val name:String, val age:Int)
data class Person(val name: String, val age:Int)
//----------------------------------------------------------
class ReadInfo2<T>(private val isShow:Boolean, private val obj: T) {
//万能对象返回器
fun getObj() = obj.takeIf { isShow }
}
//--------------------------------------------------------------
/**
* 泛型变换
* 1.类 isMap,map takeIf map是什么类型
* 2.map int->str, 最终接收是什么类型
* 3.map per->stu 最终接收是什么类型
* 4.验证是否此类型 or null
*/
class MapInfo<T>(val isMap:Boolean, val inputType: T) {
//模仿Rxjava,使用map进行变换
//T是要变换的输入类型, R是变换后的输出类型
//要是map返回的类型是R? == 有可能是R,有可能为null
inline fun <R> map(mapAction : (T) -> R) = mapAction(inputType).takeIf { isMap }
}
class MapInfo2() {
//模仿Rxjava,使用map进行变换
//T是要变换的输入类型, R是变换后的输出类型
//要是map返回的类型是R? == 有可能是R,有可能为null
inline fun <I, O> map2(isMap: Boolean = true, inputValue: I, mapActionLambda: (I) -> O) /*: O?*/ =
// mapActionLambda(inputValue).takeIf { isMap } ?: null
if (isMap) mapActionLambda(inputValue) else null
}
class KtBaseGenericityTest01 {
fun testGenericity01() {
val student1 = Student("zhangsna", 11)
val student2 = Student("lisi", 12)
//类的信息:Student(name=zhangsna, age=11)
ReadInfo(student1).showInfo()
//类的信息:Student(name=lisi, age=12)
ReadInfo<Student>(student2).showInfo()
ReadInfo<String>("字符串哦").showInfo()
ReadInfo(8888.88).showInfo()
}
fun testGenericity02() {
val student1 = Student("zhangsna", 11)
ReadInfo2<Student>(true, student1).getObj()?.run {
//如果getObj对象不为空,就会走进来
//this == getObj本身
//万能对象是:Student(name=zhangsna, age=11)
Log.d(KtBaseGenericityTest01_TAG, "万能对象是:$this")
} ?: Log.d(KtBaseGenericityTest01_TAG, "对象返回器为空")
val student2 = Student("lisi", 12)
ReadInfo2<Student>(false, student2).getObj()?.run {
//如果getObj对象不为空,就会走进来
//this == getObj本身
//对象返回器为空
Log.d(KtBaseGenericityTest01_TAG, "万能对象是:$this")
} ?: Log.d(KtBaseGenericityTest01_TAG, "对象返回器为空")
}
fun testGenericity03() {
val student1 = Student("zhangsna", 11)
val r1 = ReadInfo2<Student>(true, student1).getObj().apply{
}!!
//r1=Student(name=zhangsna, age=11)
Log.d(KtBaseGenericityTest01_TAG, "r1="+r1)
//万能对象是:Student(name=zhangsna, age=11)
val r2 = ReadInfo2<Student>(true, student1).getObj().apply {
//如果getObj对象不为空,就会走进来
//this == getObj本身
if (this != null) {
Log.d(KtBaseGenericityTest01_TAG, "万能对象是:$this")
} else {
Log.d(KtBaseGenericityTest01_TAG, "对象返回器为空")
}
} ?: Log.d(KtBaseGenericityTest01_TAG, "对象返回器为空")
/**
* 万能对象是:null
* 对象返回器为空
*/
val student2 = Student("lisi", 12)
ReadInfo2<Student>(false, student2).getObj().apply {
//如果getObj对象不为空,就会走进来
//this == getObj本身
Log.d(KtBaseGenericityTest01_TAG, "万能对象是:$this")
} ?: Log.d(KtBaseGenericityTest01_TAG, "对象返回器为空")
}
fun testGenericity04() {
val mapInfo = MapInfo(true, 88)
//int -> str
val r1 = mapInfo.map {
it.toString()//lambda最后一行作为返回值
"我的it是:"+it.toString()//lambda最后一行作为返回值
}
//testGenericity04==>r1=我的it是:88
Log.d(KtBaseGenericityTest01_TAG, "testGenericity04==>r1="+r1)
}
/**
* Student 转 Person
*/
fun testGenericity05() {
val mapInfo = MapInfo2()
val r1 = mapInfo.map2(true, Student("lisi",11)) {
//it == Student
Person(it.name, it.age)
}
//testGenericity05==>r1=true
//testGenericity05==>r1=Person(name=lisi, age=11)
Log.d(KtBaseGenericityTest01_TAG, "testGenericity05==>r1="+(r1 is Person))
Log.d(KtBaseGenericityTest01_TAG, "testGenericity05==>r1="+r1)
}
}
二:泛型约束
/**
* DESC : 泛型约束
*/
const val KtBaseGenericityTest02_TAG = "KtBaseGenericityTest02"
open class MyAnyClass(name: String) //顶级父类
open class PersonClass(name: String) : MyAnyClass(name = name) //父类
open class StudentClass(name: String, age : Int) : PersonClass(name= name) //子类
open class TeacherClass(name : String) : PersonClass(name = name) //子类
open class DogClass(name : String)//其他类
/**
* T : PersonClass
* 相当于java的T extends PersonClass
* 限制PersonClass及其子类可以使用此泛型,其他类不能使用
*/
class TestClass<T : PersonClass>(private val flag: Boolean = true, private val input: T) {
//万能对象返回器
fun getObj() = input.takeIf { flag }
}
class KtBaseGenericityTest02 {
fun testGenericity01() {
val anyClass = MyAnyClass("Any name")
val personClass = PersonClass("Person name")
val studentClass = StudentClass("zhangsan", 19)
val teacherClass = TeacherClass("Mr's Wang")
val dogClass = DogClass("HuaHua")
//报错
// val t0 = TestClass(true, anyClass)
val t1 = TestClass(true, personClass).getObj()
val t2 = TestClass(true, studentClass).getObj()
val t3 = TestClass(true, teacherClass).getObj()
//报错
// val t4 = TestClass(true, dogClass).getObj()
/**
* testGenericity01==>t1=com.xyaty.kotlinbasedemo.base06.PersonClass@d59eb6b
* testGenericity01==>t2=com.xyaty.kotlinbasedemo.base06.StudentClass@4c606c8
* testGenericity01==>t3=com.xyaty.kotlinbasedemo.base06.TeacherClass@5120361
*/
Log.d(KtBaseGenericityTest02_TAG, "testGenericity01==>t1="+t1)
Log.d(KtBaseGenericityTest02_TAG, "testGenericity01==>t2="+t2)
Log.d(KtBaseGenericityTest02_TAG, "testGenericity01==>t3="+t3)
}
}