kotlin 类的扩展属性

2022-11-13  本文已影响0人  Bfmall

类的扩展属性

package com.xyaty.kotlinbasedemo.base06

import android.annotation.SuppressLint
import android.util.Log
//扩展函数重命名
import com.xyaty.kotlinbasedemo.base06.randomItemValue as random1
import com.xyaty.kotlinbasedemo.base06.showRandomItemValue as random2

/**
 * DESC   : 扩展属性
 */

const val KtBaseExtensionFieldTest01_TAG = "KtBaseExtensionFieldTest01"

//普通属性
val myStr : String = "nihaoma"
/**
 * 背后代码
 *  @NotNull
    private static final String myStr = "nihaoma";

    @NotNull
    public static final String getMyStr() {
    return myStr;
    }
 */

//扩展属性
val String.myExtStr : String
    get() = "你好吗"

/**
@NotNull
public static final String getMyExtStr(@NotNull String $this$myExtStr) {
Intrinsics.checkParameterIsNotNull($this$myExtStr, "$this$myExtStr");
return "你好吗";
}
 */

@SuppressLint("LongLogTag")
fun String.showExtFieldInfo() :String {
    Log.d(KtBaseExtensionFieldTest01_TAG, "showExtFieldInfo==>属性值是:$this")
    return this
}


//-----------------------------------------------

/**
 * 可空类型函数扩展
 */
@SuppressLint("LongLogTag")
fun String?.showExtInfo(default: String) {
    val result = this ?: default
    Log.d(KtBaseExtensionFieldTest01_TAG, "showExtInfo==>$result")
}

/**
 * 编译器非常智能,能够检测到你做了if判断(能够对你代码逻辑检测),就知道后续类型
 */
@SuppressLint("LongLogTag")
fun String?.showExtInfo2(default: String) {
    val result = if (this == null) default else this
    Log.d(KtBaseExtensionFieldTest01_TAG, "showExtInfo2==>$result")
}

//--------------------------------------------------------

/**
 * infix 中缀表达式
 * 可以简化我的代码
 */
@SuppressLint("LongLogTag")
private infix fun <C1, C2> C1.goTo(c2: C2) {
    Log.d(KtBaseExtensionFieldTest01_TAG, "中缀表达式参数一:$this, 参数二:$c2")
}

//----------------------------------------------------------


/**
 * 扩展文件:
 * 1.扩展文件一般都是public类型,如果是private,外界无法使用
 * 2.Iterable<E> 的子类 set list都可以用,所以扩展文件是父类
 * 3.本次扩展函数是随机取第一个元素返回
 *
 */
fun <E> Iterable<E>.randomItemValue() = this.shuffled().first()

@SuppressLint("LongLogTag")
fun <E> Iterable<E>.showRandomItemValue() {
    Log.d(KtBaseExtensionFieldTest01_TAG, "showRandomItemValue==>"+this.shuffled().first())
}


class KtBaseExtensionFieldTest01 {

    @SuppressLint("LongLogTag")
    fun testExtensionField01() {
        val info : String = "what are you doing ?"
        //info=你好吗
        Log.d(KtBaseExtensionFieldTest01_TAG, "info="+info.myExtStr)

        //showExtFieldInfo==>属性值是:what are you doing ?
        //showExtFieldInfo==>属性值是:what are you doing ?
        info.showExtFieldInfo().showExtFieldInfo()

        //showExtFieldInfo==>属性值是:你好吗
        //showExtFieldInfo==>属性值是:你好吗
        info.myExtStr.showExtFieldInfo().showExtFieldInfo()

        info?.showExtInfo("我很好")
        info?.showExtInfo2("我很好")

    }

    /**
     * infix 中缀表达式
     */
    fun testInfix01() {
        //map中的中缀表达式
        mapOf<String, String>("xiaowang" to "19岁")
        mapOf<String, Int>("xiaoming" to 18)

        //下面是我们自己写的中缀表达式 goTo

        /**
         * 中缀表达式参数一:xiaohong, 参数二:18
         * 中缀表达式参数一:xiaoli, 参数二:19
         * 中缀表达式参数一:xiaosan, 参数二:18
         */
        "xiaohong".goTo("18")//不使用infix可以编译通过,不是中缀表达式
        "xiaoli".goTo(19)//不使用infix可以编译通过,不是中缀表达式
        //以下的中缀表达式不加infix,编译不过,加上后就是中缀表达式
        "xiaosan" goTo 18
    }

    /**
     * 扩展文件
     */
    fun testIterable01() {
        val set : Set<String> = setOf("haha", "xixi", "gaga")

        val list : List<Double> = listOf(18.88, 38.88, 58.88)

        //Iterable的子类set,list调用父类Iterable的扩展函数


//        set.randomItemValue()
//        list.randomItemValue()

//        set.showRandomItemValue()
//        list.showRandomItemValue()


        /**
         * 扩展函数重命名
        import com.xyaty.kotlinbasedemo.base06.randomItemValue as random1
        import com.xyaty.kotlinbasedemo.base06.showRandomItemValue as random2
         */
        set.random1()
        list.random1()

        set.random2()
        list.random2()

        //重命名扩展函数
    }
}
上一篇 下一篇

猜你喜欢

热点阅读