In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will share with you about the grammatical features of kotlin. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
At the Google Kotlin O 2017 developer Conference, Google announced the formal inclusion of Kotlin as the official first-level development language (First-class language) of Android programs. As an Android developer, if you want to get familiar with this language, the first step is to start with grammar.
Before we do that, we need to know how to write an Android application using Kotlin. For Android Studio 3.0, we can simply check the Include Kotlin support option when creating the project; for versions prior to 3.0, we need to install the Kotlin plug-in and configure gradle manually as follows
Add the following code to the gradle of app
Apply plugin: 'kotlin-android'apply plugin:' kotlin-android-extensions'
Add the following code to the gradle of project
Ext.kotlin_version = '1.1.2-3'classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
Kotlin defines variables
There are two kinds of variable definitions in kotlin, val and var, in which val is equivalent to a variable modified by final in Java (read-only), which is generally a constant, and var is generally a variable.
Kotlin's variable definition supports type inference when assigning values, and all variables are modified to "not null" by default and must be explicitly added after the type. The modifier can only be assigned to null.
When we write code, we should try our best to design the variable as non-nullable, so that a lot of problems will be reduced in the later operation of the variable.
Kotlin function extension
The specific syntax is the fun + type. Function (parameter)
Fun Context.toast (message: String, length: Int = Toast.LENGTH_SHORT) {Toast.makeText (this, message, length) .show ()}
Kotlin Parcelable serialization
Package com.john.kotlinstudyimport android.os.Parcelimport android.os.Parcelable/** * Java Bean data entity class * Created by john on 17-5-24. * / data class UserBean (var name: String, var id: String): Parcelable {constructor (source: Parcel): this (source.readString (), source.readString ()) override fun describeContents (): Int {return 0} override fun writeToParcel (dest: Parcel Flags: Int) {dest.writeString (this.name) dest.writeString (this.id)} companion object {@ JvmField val CREATOR: Parcelable.Creator = object: Parcelable.Creator {override fun createFromParcel (source: Parcel): UserBean {return UserBean (source)} override fun newArray (size: Int): Array {return arrayOfNulls (size)}
Companion keyword interpretation
Unlike Java or clips, in Kotlin, Class does not have static methods, and in most cases, it is recommended to use functions of package-level instead of static methods.
If you need to write a function (such as a factory function) that can be accessed inside the Class without instantiating Class, you can declare it as a real name Object within the Class.
In addition, if you declare a companion object in Class, all members in the object will be equivalent to using the static modifier in the Java/C# syntax, and these properties or functions can only be accessed externally through the class name.
@ JvmField annotation function
Instructs the Kotlin compiler not to generate getter / setter for this property and expose it as a field.
If you need to expose the Kotlin property as a field in Java, you need to annotate it with the @ JvmField annotation, which will have the same visibility as the underlying property.
Kotlin authoring tool class
In Java, we will encapsulate some commonly used functions into tool classes, which are actually extensions to the functions of common classes such as String,Collection,IO. The utility class methods and variables we write are static. Because we just want to call these methods and do not need to involve any properties and variables in the utility class, there is no need to instantiate (new). Since instantiation is no longer necessary, we can just use static.
Package com.john.kotlinstudyimport android.content.Contextimport android.widget.Toast/** * Toast tools * Created by john on 17-5-24. * / object ToastUtils {fun toast (context: Context, message: String) {Toast.makeText (context, message, Toast.LENGTH_SHORT) .show ()}}
Kotlin Activity Jump
We set the click event in MainActivity, jump to another Activity, and pass the data over.
Package com.john.kotlinstudyimport android.content.Contextimport android.support.v7.app.AppCompatActivityimport android.os.Bundleimport android.widget.Toastimport kotlinx.android.synthetic.main.activity_main.*class MainActivity: AppCompatActivity () {override fun onCreate (savedInstanceState: Bundle?) {super.onCreate (savedInstanceState) setContentView (R.layout.activity_main) test_tv.text = "hello kotlin" test_tv.setOnClickListener {ToastUtils.toast (this, "hello kotlin") val user = UserBean ("zhang") User.id = "100" SecondActivity.navigateTo (this, user)}} fun Context.toast (message: String, length: Int = Toast.LENGTH_SHORT) {Toast.makeText (this, message, length). Show ()}}
Then create a new SecondActivity that provides a static method for Activity jump. As you all know, the advantage of this is that the caller knows what parameters are needed without looking at the source code. If you write according to java, you will find that there is no keyword static! Don't panic, you can use a companion object here, which is the object that accompanies the declaration cycle of this class.
Package com.john.kotlinstudyimport android.content.Contextimport android.content.Intentimport android.os.Bundleimport android.support.v7.app.AppCompatActivityimport kotlinx.android.synthetic.main.activity_second.*/** * Jump Activity test class * Created by john on 17-5-24. * / class SecondActivity: AppCompatActivity () {override fun onCreate (savedInstanceState: Bundle?) {super.onCreate (savedInstanceState) setContentView (R.layout.activity_second) val user = intent.getParcelableExtra (EXTRA_KEY_USER) user_name_tv.text = user.name ToastUtils.toast (this, user.id)} / / create a companion object companion object {/ / extra key val EXTRA_KEY_USER = "extra.user" fun navigateTo (context: Context) User: UserBean) {val intent = Intent (context, SecondActivity::class.java) intent.putExtra (EXTRA_KEY_USER, user) context.startActivity (intent)}} Thank you for reading! This is the end of this article on "what are the grammatical features of kotlin?". 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, you can 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.