How to implement static methods like Java or C # in Kotlin, this paper summarizes several methods, including package-level functions, associated objects, extension functions, and object declarations. This requires people to choose according to different circumstances.
You can find a lot of such articles on the Internet, the official recommendation is the package-level function, also some people say with the associated object (companion Class). These are good choices, but not perfect, and we have better choices in different situations. I've summed up several methods, including package-level functions, associated objects, extension functions, and object declarations. This requires people to choose according to different circumstances.
One, packet-level functions
Unlike Java and C #, Kotlin can declare functions directly inside a package. The practice is the same as in the class, there is not much to say, it is indeed a very good choice. Applies to methods where a function does not need to share data in a class without a package.
Second, the associated object
Semantically, the associated function is closest to the static method in Java, so it is possible to implement all the contents of a static class in Java with the associated object. But using static methods in Java is sometimes not a choice, and the associated object in Kotlin is just one of our choices. Let me introduce you to the associated objects.
When you are using Java, do you not know when the static variables and methods in the Java class are initialized? When the static method is first called or when the corresponding Java class is loaded? The answer is when the Java class is loaded (that is, if you have called the instance variables and methods in the Java class, even if you have not used static variables and methods, the static variables have been initialized). This phenomenon is aptly described with the accompanying.
In Kotlin, there are two kinds of things in a class, one is instance variables and methods in Java class, the other is static variables and methods in Java class. Kotlin the static instances and methods uniformly packaged into a companion class, that's what it looks like. An example is given below:
Fun Main (args:array<string>) {books.getbestsellers ()}class Books (Var name:string, Val page:int) {Fun getwor DCount () =page*100 Companion object combooks{ var bestsellers=arrayof ("Harry potter\r\t", "Lord of the Rings\r\ T ") Fun getbestsellers () { bestsellers.foreach{v->println (v)}}} }
The associated class is declared with companion, which is loaded in the class where the associated object is located, the associated object is initialized, as is the Java static member. It can be anonymous or the same as the class name that contains him. There are two types of calls: Books.ComBooks.getBestSellsers()?
orBooks.getBestSellsers()?。
Third, extension function
In Java, we often write utils classes, which are often used to match the functionality of an object to its own program. The methods are mostly static, for example:
public class Utils {public static Boolean IsEmpty (String string) { return String! = null && string.lengt H () = = 0; } public static Boolean Isweakempty (String string) { return IsEmpty (String) && String.Trim (). Length () = = 0;< c15/>}}
We can certainly use the two methods above to implement these static methods, but we have a better approach.
Fun String.isempty () = this! = null && this.length = = 0;fun String.isweakempty () = This.isempty () && this.t Rim (). length = = 0
The above two lines of code extend two functions to the string class, and the two functions can be called the same as their native functions, and the code is beautiful. In fact, the extension function does not modify the inside of the string class, but also to string added two static functions, but compared to Java Utils class, readability has been greatly improved.
Iv. Declaration of objects
Continue to consider the above Utils class, all the methods in this class (and sometimes there may be variables) are static, this method is not necessary to instantiate, in Java we often declare this kind of class as static classes, in the Kotlin in this case there is no good corresponding solution? Is there a better solution than the associated object? There is, of course, an object declaration.
Object declaration is very well understood, is to use the OBJECT keyword to declare an object, the object can be used in variables can also have methods, such as:
Object appinfo{ var AppName = "Kotlin Message" var appauthor = "Riley Ge" Fun tosimplestring () { println ( "AppName: $AppName, Appauthor: $AppAuthor") }}
Discover Kotlin's object is really powerful! One thing to note is that AppInfo is deferred for the first time it is accessed, meaning that appinfo is not initialized when the object is declared.
V. Summary
The
has said so many ways, now no one is worrying about Kotlin no static method, Kotlin not just because he can do better. And Kotlin also give everyone more choice, we can choose the appropriate method according to their actual situation, so that their code efficient and beautiful.