Today in Csdn home accidentally saw a post JetBrains officially released Kotlin 1.0:JVM and better language on Android
After reading, feel Kotlin grammar is very concise, there are a series of dynamic language features, lambda expressions, high-order functions, closures, even static language type check, the most core point, write the same function, Kotlin code is much less.
Here is a data class for Java
Public classArtist {Private LongIdPrivateString name;PrivateString URL;PrivateString Mbid; Public Long getId() {returnId } Public void setId(LongID) { This. id = ID; } PublicStringGetName() {returnName } Public void SetName(String name) { This. name = name; } PublicStringGETURL() {returnUrl } Public void SetUrl(String URL) { This. url = URL; } PublicStringGetmbid() {returnMbid; } Public void Setmbid(String mbid) { This. mbid = Mbid; } @Override PublicStringtoString() {return "artist{"+"Id="+ ID +", Name= '"+ name +' \ '+", Url= '"+ URL +' \ '+", mbid= '"+ Mbid +' \ '+'} '; }}
If C # 3.0 and above, you will not see a bunch of Set (), Get () method, compared to more concise
public Class xxxx{ publicint AAAA {set;get;} publicstring BBBB {set;get;} ......}
But Kotlin's code is simply too concise to be better than C #
class Artist( var id: Long, varString, varString, varString)
As an example, when developing an Android app in Java, it is always called Findviewbyid to get the controls in the activity.
private ShimmerTextView mActionBarTitle; mActionBarTitle = (ShimmerTextView) view.findViewById(R.id.tv_shimmer);
This kind of writing is very cumbersome and annoying, you have to define a control type, call Findviewbyid to get the control object, but in Kotlin Findviewbyid method is completely abandoned, for example, there is an activity inside a textview, Its ID is mymessage, so you can import it in Kotlin
import kotlinx.android.synthetic.another_activity.myMessage
Now you can access the TextView by using the ID, just as the TextView is a property of the activity class. For example, modify the string displayed by TextView:
myMessage.setText("Hello")
What if you want to access a lot of controls? Importing is still a hassle, this time you can choose to import all the controls on the activity
import kotlinx.android.synthetic.another_activity.*
The above example I have a basic excerpt from the following articles
The application of Kotlin in Android engineering
Developing Android Apps with Kotlin (I): Introduction
Developing Android Apps with Kotlin (II): Creating a new project
Developing Android Apps using Kotlin (III): extension functions and default values
Developing Android Apps with Kotlin (IV): Custom views and Android extensions
MVVM Development Framework written in Kotlin language
Meet the Kotlin language, Android platform for Swift