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 > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to use kotlin top-level functions and function expressions. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Top-level function
Unlike functions that can only be defined in each class in Java, Kotlin adopts the practice in JavaScript and can define functions anywhere in the file, which is called top-level function.
After compilation, the top-level function becomes a static function under the file class. For example, the joinToString function defined under the file name join.kt can be called through JoinKt.joinToSting, where JoinKt is the compiled class name.
/ / compiled into a static function / / filename join.ktpackage stringsfun joinToString (): String {...} / * Java * / import strings.JoinKt;JoinKt.joinToSting (....)
Take a look at the compiled effect of the above function: / / decompiled result after being compiled into a class file
/ / decompilation result @ NotNullpublic static final String joinToString (@ NotNull Collection collection, @ NotNull String separator, @ NotNull String prefix, @ NotNull String postfix) {Intrinsics.checkParameterIsNotNull (collection, "collection"); Intrinsics.checkParameterIsNotNull (separator, "separator"); Intrinsics.checkParameterIsNotNull (prefix, "prefix"); Intrinsics.checkParameterIsNotNull (postfix, "postfix"); StringBuilder sb = new StringBuilder (prefix); int index = 0; for (Iterator var7 = (Iterable) collection). Iterator () Var7.hasNext (); + + index) {Object element = var7.next (); if (index > 0) {sb.append (separator);} sb.append (element);} sb.append (postfix); String var10000 = sb.toString (); Intrinsics.checkExpressionValueIsNotNull (var10000, "sb.toString ()"); return var10000 } / / default function values public static String joinToString$default (Collection var0, String var1, String var2, String var3, int var4, Object var5) {if ((var4 & 2)! = 0) {var1 = "";} if ((var4 & 4)! = 0) {var2 = "[";} if ((var4 & 8)! = 0) {var3 = "]";} return joinToString (var0, var1, var2, var3)
Next, let's take a look at a very important feature in Kotlin, the extension function.
Extended function
An extension function is a member function of a class, but it is defined outside the class
Extension functions cannot access private or protected members
Extension functions are also compiled into static functions
Expression function body
Take a look at the concepts related to the function declaration through the following simple example. The keyword of the function declaration is fun, which is, well, simpler than JS's function.
In Kotlin, the parameter type is placed in the variable: after that, so is the function return type.
Fun max (a: Int, b: Int): Int {if (a > b) {return a} else {return b}}
Of course, Kotlin has the function of type derivation, and if you can derive the type from the function expression, you can also not write the return type.
But the above is still a bit tedious and simple. In Kotlin, if is an expression, that is, it has a return value, so you can return directly. In addition, only one line and one sentence in the judgment formula can omit the braces:
Fun max (a: Int, b: Int) {return if (a > b) an else b}
Can it be any easier? Yes, if is an expression, so it can be returned through the body of the expression function:
Fun max (a: Int, b: Int) = if (a > b) an else b
In the end, all it takes is one line of code.
This is the end of this article on "how to use kotlin top-level functions and function expressions". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.