kotlin泛型、反射获取泛型的属性值

kotlin泛型

简介

这里介绍kotlin泛型的常用的情况

代码

class Box<E>(t: E) {
    /***** 第一种 传入不同的类型**然后给 Box类 属性赋值*********/
    var vaule = t //这里的vaule可以接收Int或者String类型

    /***** 第二种 从方法中传入不同的类型,然后根据传入的类型运行不同的方法*********/
    fun <T> doPrintln(content: T) {//在类上面可以不用 <T>(t: T) 也能编译通过
        when (content) {//这里的参数不能使用传入来的 t,要使用的话,就用 vaule
            is Int -> Log.d("WY+", "方法中传入了Int类型: $content")
            is String -> Log.d("WY+", "方法中传入了String类型:${content.toUpperCase()}")
            is Boolean -> Log.d("WY+", "方法中传入了Boolean类型:$content")
        }
    }

    /***** 第三种 传入List<>里面装不同类型*********/
    fun <T : Comparable<T>> listshow(list: List<T>) {
        Log.d("WY+", "List列表(第一个): ${list[0]}")
    }

    /***** 第四种 传入多个类型的参数*********/
    fun <T> anyType(t1: T, t2: T, t3: T, list: List<T>) {
        Log.d("WY+", "t1: $t1=t2:$t2=t3:$t3=list:${list[0]}")

    }

    /***** 第五种 多类型参数*********/
    fun <T,U> addRectangle(a: T,b: U): U{
        return b
    }

    /***** 第六种 泛型约束**  Number代表Double,Long,Int,Float,Char,Short,Byte*******/
    fun <T: Number> isEqual(a: T,b: T): Boolean{
        return (a == b)
    }
}

在activity中使用:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        /********* 1 *传入不同的类型**然后给 Box类 属性赋值***********/
        var boxInt = Box<Int>(10)//传入Int类型
        var boxString = Box<String>("Runoob")//传入String类型
        Log.d("WY+", "输出:" + boxInt.vaule + "/" + boxString.vaule)

        /********* 2 *方法中传入不同的类型***然后根据传入的类型运行不同的方法 ***********/
        boxInt.doPrintln(true)

        /********* 3 *方法中传入List<>里面装不同类型**************/
        boxInt.listshow(listOf(1, 2, 3))
        boxInt.listshow(listOf("a", "b", "c"))

        /********* 4 *任意类型的定义**************/
        boxInt.anyType(1,"aa",true,listOf(3, 2, 1))

        /********* 7 *任意*类型的类**************/
        val a1: A<*> = A(12, "String", true)
        //使用数组
        val arrayList: ArrayList<*> = arrayListOf("String", 1, 1.2f, true)
        for (item in arrayList){
            println(item)
        }

    }
}

A类

/**
 * 可以定义任意类型
 * val a1: A<*> = A(12, "String", true)
 */
class A<T>(val t: T, val t2 : T, val t3 : T) {

}

题外

封装模块的时候遇到一种棘手情况:在一个依赖模块module_ble里提供一个对外的方法printFun,在app模块里调用printFun,把PickSelectBill作为入参,并且在printFun方法里要用到PickSelectBill里的属性值
难点:printFun方法里拿不到app里的PickSelectBill类,导致不能强转换
解决方案:
1.使用反射的方式

2.(推荐)使用Gson先转换为json字符串,然后再转为对应的bean。(前提是PickSelectBill和PrintBean的属性要一样)
使用Gson 把app的PickSelectBill转换为module_ble里的PrintBean
在这里插入图片描述
app的main里调用
在这里插入图片描述
方式一:使用反射来获取泛型里的属性

// 1.利用反射
    inline fun <reified T> printProperty(obj: T, propertyName: String) {
        try {
            val property = T::class.java.getDeclaredField(propertyName)
            property.isAccessible = true
            val value = property.get(obj)
            println(value)
        } catch (e: Exception) {
            throw Exception("类型转换错误:${e.message}")
        }
    }

方式二:使用Gson转换的方式

 // 2.先转成json字符串,再转成PrintBean
    inline fun <reified T : Any> toPrintBean(obj: T): PrintBean? {
        try {
            val gson = Gson()
            val jsonStr = gson.toJson(obj)
            return gson.fromJson(jsonStr, PrintBean::class.java)
        } catch (e: Exception) {
            throw Exception("类型转换错误:${e.message}")
        }
    }

这样就可以解决在module_ble拿不到PickSelectBill的问题

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wy313622821

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值