Get into the habit of writing together! This is the 7th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .
1. @JvmName
Modify the method name
Just look at the example:
@JvmName("testCopy")
fun test(name: String, age: Int) {
}
复制代码
Decompile directly into java code to see:
The final generated method name is testCopy instead of test
2. @get:JvmName
, @set:JvmName
modify the attribute name
@get: JvmName("getSource")
@set: JvmName("setSource")
var mData: String = ""
复制代码
Decompile directly into java code to see:
For val
properties you can only use@get: JvmName
3. String is empty and used isNullOrEmpty
to avoidTextUtils.isEmpty()
Let's compare these two ways of writing:
It can be seen that if you use isNullOrEmpty
an empty String, name.length
you will not report an error when you call it, but if you use it, you TextUtils.isEmpty()
will report an error. You need to force the declaration to name!!
not be empty`
Why does the first one not report an error, look at isNullOrEmpty
the source code:
@kotlin.internal.InlineOnly
public inline fun CharSequence?.isNullOrEmpty(): Boolean {
contract {
returns(false) implies (this@isNullOrEmpty != null)
}
return this == null || this.length == 0
}
复制代码
The core is isNullOrEmpty
that there is one in the method body contract
, this will help the compiler to tell whether the String is empty, so that when the compiler is called name.length
, the compiler can infer that the String is not empty, so there is no need for the program to force the declaration of name!!
non-empty.
4. Ignore case comparisonequals
A general case comparison is as follows:
val res = "aa".toLowerCase(Locale.ROOT) == "AA".toLowerCase(Locale.ROOT)
复制代码
Decompile into java code and see:
As you can see, "aa".toLowerCase(Locale.ROOT)
assign to a local variable var6
, "AA".toLowerCase(Locale.ROOT)
assign to another local variable var7
, and then compare.
This way of comparison will create two additional local String variables, so it is recommended to use equals
substitution, where the second parameter can specify to ignore case comparison
val res = "aa".equals("AA", ignoreCase = true)
复制代码
Decompile:
As you can see, the implementation is very simple and does not create additional local variables
5. Operator overloading get
,set
get
class Pro {
operator fun get(content: String): String {
return content.repeat(10)
}
}
复制代码
Then it can be used like this:println(Pro()["blue"])
set
operator fun set(key: String, value: String) {
}
复制代码
set
Operator overloading requires at least two parameters to be passed in, which are used as follows:Pro()["key"] = "value"
There are many usage scenarios for these two operators, such as the read and write encapsulation of SharedPreference in Android. For details, please refer to the article: 3. Combination of Delegate and SharedPreference
There are many other operator overloaded functions, such as plus
corresponding to "+", contains
corresponding to "in", etc., which are commonly used in daily development, and you can use them flexibly according to specific scenarios.