Kotlin,记录在kotlin官方文档的学习,后续会继续补充
package main
fun main() {
println("Hello,world!")
}
annotations:
single-line:
multi-lines:
import:
import package
types:
Base types:
Int
Byte
Short
Long literalL
Float
Double
Char single-quote
Boolean true/false
Compound types:
String double-quotes
the first element in the expression is a string:
string+int+float+double=>string
Multi-lines:
"""
"""
\n,\t,...
Array
Any
exceptions:
throw Exception(prompt)
variable declarations:
immutable variable:
val variable:Type=value
mutable variable:
var variable:Type=value
Sometime type can be inferred:
immutable variabe:
val variable=value
mutable variable:
var variable=value
operators:
+,-,*,/,%
+=,-=,*=,/=,%=
++,--
conditions:
item in iterables
item !in iterables
string templates:
"$var ${expr}"
in templates,$var and ${expr} can be parsed the real value.
if expression:
if (condf) {
operf
}
if (condf) {
operf1
} else {
operf2
}
if (condf1) {
operf1
} else if (consf2) {
operf2
} else {
operf3
}
val variable=if (condf) value1 else value2
when expression:
when (expr) {
result1 -> value1 or operf1
result2 -> value2 or operf2
in iterables -> value3 or operf3
!in iterables -> value4 or operf4
is Type -> value5 or operf5
else -> valueN or operfN
}
when {
condf -> operf1
else -> operf2
}
equivalent to:
match expr:
case result1:
operf1
case result2:
operf2
case _:
operf_n
for loop:
for (i in begin..end) {
operf(i)
}
for (i in begin..<end) {
operf(i)
}
for (i in begin..end step k) {
operf(i)
}
for (i in end downTo begin) {
operf(i)
}
for (i in end downTo begin step k) {
operg(i)
}
for (c in str) {
operf(c)
}
label@ for (i in iterables) {
operf
break@label
}
equivalent to:
for i in range(begin,end,step):
operf(i)
for i in iterable:
operf(i)
iterable can be collections,lists.
while loop:
while (condf(i)) {
operf
operf(i)
}
do {
operf
} while (condf)
data structures:
val list=listOf(item sequence)
val map=mapOf(key to value sequence)
val array=arrayOf(item sequence)
map[key]=>value
map[key]=value
for (item in list) {
operf(item)
}
for (index in list.indices) {
operf(index)
}
for ((k,v) in map) {
operf(k,v)
}
for (item in array) {
operf(item)
}
for (index in array.indices) {
operf(index)
}
for ((i,v) in array.withIndex()) {
operf(i,v)
}
try-catch-finally expression:
try {
operf1
} catch(e:Error) {
operf2
}
try {
operf1
} catch(e:Error) {
operf2
} finally {
operf3
}
functions:
fun main(args:Array<String>) {
operf
}
it can also be written by following simple way if Kotlin>=1.3:
fun main() {
operf
}
fun function(params:paramTypes=default):ReturnType {
operf
return value
}
fun function(params:paramTypes=default):Uint {
operf
}
function(args)
returns:
when expression
if expression
single-expreesion functions:
fun function(params:paramTypes=default):Returntype=expression
null deals:
println(variable?.property)
equivalent to:
if variable is not null:
print(variable.property)
println(variable?.property ?: value)
equivalent to:
if variable is not null:
print(variable.property)
else:
print(value)
result=variable?.property ?: run {
operf
value
}
println(result)
equivalent to:
if variable is not null:
print(variable.property)
else:
operf
print(value)
val variable=expr ?: operf
equivalent to:
if expr is not null:
variable=expr
else:
operf
variable?.let {
operf
}
equivalent to:
if variable is not null:
operf
object-oriented programming:
class CanNotInheritClass {
val property:Type=value
var property:Type=value
fun canNotOverrideMethod(params:paramTypes):ReturnType {
operf
return value
}
}
open class CanInheritClass {
val property:Type=value
var property:Type=value
fun canOverrideMethod(params:paramTypes):ReturnType {
operf
return value
}
}
data class DataClassName constructor(
var property:Type sequence
)
inherits:
class SonClass:ParentClass() {
}
instance:
var cls =ClassName()
var cls=ClassName().apply {
property1=value1
property2=value2
sequence
}
calls:
cls.method(args)
call multiple methods on an object instance (with)
with (object) {
method1(args)
if
for
while
when
method2(args)
methods(args)
}
tips:
swap two variables:
a=b.also {b=a}
fun function() {
if (condf) return
}
String.format(format_string,variable sequence)