Extension Methods artistic exploration of Kotlin and property broker

Extension Methods

Kotlin support extension methods, extension method is a very interesting thing

Or give more intuitive chestnuts

For example, we want to print 10 "abc" string, most Yung think of using a loop. So as a method to separate out, such that

fun String.copyContent(times: Int): String{

    var string = StringBuilder()
    for (i in 0 until times){
        string.append(this)
    }
    return string.toString()
}

You take a closer look at this method, and it is a little common methods are not the same, that is, before it has a method name String.

So write what good is it? Look at the following call operation you will understand

println( "abc".copyContent(10))

The above result is the output string 10 abc

I have not found very simple, direct method string point on ok

The method defined by the above way is an extension method, pay attention to the expansion of this method is that the caller instead of "abc"

In fact, the above chestnuts can also write more show

How to operate it a show that using operators

operator fun String.times(times: Int): String{
    var string = StringBuilder()
    for (i in 0 until times){
        string.append(this)
    }
    return string.toString()
}

Here is the time to witness the miracle of the

 println("abc"*10)

I do not know of a String class look will think there is such a kind of operation it, ah ha ha, we can say this wave of operations show a very

Property Broker

What property broker, agent mode?

We look at a chestnut:

Kotlin to delay loading of constants when he used the property broker, it is this:

val lazyValue: String by lazy {
    println("computed!")
    "Hello"
}

fun main() {
    println(lazyValue)
    println(lazyValue)
}

Output

computed!
Hello
Hello

You can see the computed! Output only once, suggesting that it is indeed in constant use this time before going to the assignment.

The agency said the property back, but in fact it is through internal realization of lazy this property broker

But it delayed loading of how to achieve it through a property broker? Here I did not say, because is not the point, nor is it I cite this purpose chestnuts, I wanted you to feel the power of the property broker, which put all the concrete realization of a property are handed over to do on the outside He said to be transparent, which is very comfortable.

Here to talk about how we are to realize the property broker?

Or give chestnuts:

Implement and operate an assignment of values ​​to variables by property broker way (chestnut is very simple, the key to achieve)

Delegate class property broker

class Delegate{

    private var value: String ?= null

    operator fun getValue(thisRef: Any?, property: KProperty<*>): String{
        println("$thisRef, thank you for delegating '${property.name}' to me!")
        return value?: ""
    }

    operator fun setValue(thisRef: Any?, property: KProperty<*>, value: String) {
        this.value = value
        println("$value has been assigned to '${property.name}' in $thisRef.")
    }

}

You can see the proxy class has two methods, setValue () and getValue ()

Note: If val is a constant, then only need to implement getValue can; if it is a variable var, it would need to achieve setValue () and getValue () two methods

By property broker function

    var de = Delegate()
    var b: String by de
    b = "10"
    println(b)

Output

10 has been assigned to 'b' in null.
null, thank you for delegating 'b' to me!
10

The whole process can be seen that the output is achieved through the properties Delegate proxy class

The chestnut is simple, but look beneath the surface nature, we only need to function can be achieved through a property, as to how to achieve for us is transparent.

Reproduced in: https: //www.jianshu.com/p/1f0be129c239

Guess you like

Origin blog.csdn.net/weixin_33859665/article/details/91303059