In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people don't understand the knowledge points of this article "How to Use Double Colons in Kotlin", so Xiaobian summarizes the following contents for everyone. The contents are detailed, the steps are clear, and they have certain reference value. I hope everyone can gain something after reading this article. Let's take a look at this article "How to Use Double Colons in Kotlin".
The double colon operator in Kotlin means that one method is passed as an argument to another method for use. In layman's terms, it refers to a method. Let's look at an example:
fun main(args: Array) { println(lock("param1", "param2", ::getResult))}
/** * @param str1
Parameter 1 * @param str2
Parameter 2 */fun getResult(str1: String, str2: String): String = "result is {$str1 , $str2}"
/** * @param p1
Parameter 1 * @param p2
Parameter 2 * @param method Method Name */fun lock(p1: String, p2: String, method: (str1: String, str2: String) -> String): String { return method(p1, p2)}
It should be noted here that when the third parameter of the lock function is passed into the method, it is necessary to ensure that the number of parameters, type, and return value are consistent with its formal parameters.
Output:
result is {param1 , param2}
If we need to call one of the methods in another Class:
Written as:
fun main(args: Array) { var d = Test() println(lock("param1", "param2", d::getResult))}
When we use a double colon in a method of a Class to invoke an internal method of the current Class, the method is:
class Test1 { fun isOdd(x: Int) = x % 2 != 0 fun test() { var list = listOf(1, 2, 3, 4, 5) println(list.filter(this::isOdd)) }}
In general, we call the method of the current class this can be omitted, the reason why it cannot be omitted here is
To prevent scope confusion, functions called by:: must use qualifiers, such as this, if they are member functions of a class or extension functions.
If you write isOdd outside of class (global), you can omit the qualifier here.
The above is the content of this article about "how to use double colons in Kotlin". I believe everyone has a certain understanding. I hope the content shared by Xiaobian will be helpful to everyone. If you want to know more relevant knowledge content, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.