Kotlin offers a number of ways to help you develop quickly. After learning, found that there are these methods, really is too convenient. You can take a look, and if you see anything that you can use, just let me know, let me learn together, let me improve together.
Run of the inline extension function. Directly access the properties and methods of the object, eliminating repeated references to the object.
fun main(args: Array<String>) {
val user = User("Kotlin", 1)
val result = user.run {
println("my name is $name, I am $age years old.")
1000
}
println("result: $result"Holder.tv?. SetText (name) holder.agetv?. SetText (age.tostring ())}Copy the code
Inline extension function lets, which do what {} does only when the called object is not null. Definition: The let extension function is actually a scope function. When you need to define a variable in a particular scope, the let function is also used to avoid writing null operations.
Eg: the object? .let{// Execute only when object is not nullletThe body of the function, notice the one? //it. Todo ()// It will be used to represent object}Copy the code
A common scenario is to use the let function to uniformly nullable a nullable object, or to specify the scope of a variable.
The inline with function returns the object itself that was passed in. Definition: it takes an object and an extension function as its arguments, and then makes the object extend the function. This means that all of the code we wrote in parentheses is an extension function of the object (the first argument), and we can use all of its public methods and attributes just as we did for this. We can call the class’s methods without repeating the class name. For example, in RecyclerView onBinderViewHolder, an object needs to be processed uniformly.
val result = with(person) {
println("my name is $name, I am $age years old") 1000 } with(item){ holder.tv? .settext (name)// Directly access the properties and methods of item holder.agetv? .setText(age.toString()) }Copy the code
The apply extension function is inline, and the apply function returns the object itself. Apply is used when an object instance is initialized to assign values to attributes in the object. For example, the View needs to be treated uniformly when the XML layout is inflate out, or some data models may be used in the process of transformation and instantiation of the View Model during development.
mSheetDialogView = View.inflate(activity, R.layout.biz_exam_plan_layout_sheet_inner, null).apply{
course_comment_tv_label.paint.isFakeBoldText = true
course_comment_tv_score.paint.isFakeBoldText = true
course_comment_tv_cancel.paint.isFakeBoldText = true
course_comment_tv_confirm.paint.isFakeBoldText = true
course_comment_seek_bar.max = 10
course_comment_seek_bar.progress = 0
}
Copy the code
The inline extension function also returns the object itself that was passed in. Similar to let.
val result = "testLet".also {
println(it.length)
1000
}
println(result)
Copy the code
Repeat function: The number of times a piece of code is repeated. For example, if I wanted to print Hello World three times, it would look like this:
repeat(3) {
println("Hello world")} ` ` `Copy the code