Keywords in kotlin that are consistent with Java usage include: override, while declarations
· Open in Kotlin a class needs to be inherited by another class, you need to use the Open keyword
Open Class Openclass {}
· is judgment type
Fun testis () {
var a:any? = null;
A = 3L
if (A is int) {
println (' A is int ')
} else if (A is long) {
println (' A is Long ')
} else {
P Rintln ("a not int or long")
}
}
· When the equivalent of Swith, we use when to optimize the previous code
Fun TestWhen1 () {
var a:any? = null;
A = 3L when
(a) {is
int-> println (' A is int ') is
Long-> println (' A is long ')
else-> print ln ("A not int or long")
}
}
Fun TestWhen2 () {
var a = 2;
When (a) {in
1..5-> println (' A in 1~5 ') in the 6..10-> println
("A In 6~10")
else-> println ("a > 10 ")
}
}
· As coercion type conversion
var Context.app:KotlinApplication get
() {return
ApplicationContext as Kotlinapplication
}
Set ( Value) {
app = value
}
· In expression within range,.. Represents a range
Fun Testin () {
var a = 3
if (a in 1..5) {
println (' A in 1~5 ')
}
}
· For basic and Java, the following code is equivalent to Fori in Java
For (i in list.indices) {
println (List.get (i))
}
The following code corresponds to a foreach in Java
For (item in list) {
println (item)
}
There are also foreach in the Kotlin, but not a keyword, as follows
List.foreach {
println (it)
}
· Lateinit literally can be seen, used for deferred initialization, when a VAR cannot be null, but the initial unassigned, can be decorated with lateinit, if the first time to use the lateinit decorated variable still does not have a value, throw an exception. Lateinit can only be used to modify Var and not to modify Val.
Lateinit var age:string
· The by lazy{} is used to delay initialization of a Val, which executes the initialization code in lazy before using the constant for the first time
Val Prefs by lazy {context.getsharedpreferences ("Kotlinlearndemo", Context.mode_private)}
· Internal are internal modified variables or classes that are visible inside the module.
. Inner represents the inner class
· init{} is equivalent to a free block in Java
init {
println ("Initialize")
}
· inline defines an inline function
· <in xclass> is the equivalent of Java. Super xclass>
· <out xclass> is the equivalent of Java. Extend xclass>
This article Code link: Https://github.com/sunflowerseat/KotlinDemo
Temporarily write these, and then add, the content of the article if there are errors welcome correction.
Have any questions about the contents of this article welcome Gathen discussion: 283272067
Next: Kotlin for Android-Combat Records < A;