Kotlin入门基础1--一句话教程
admin
2023-02-17 10:20:02
0

1,定义函数

fun 函数名(参数名:类型,参数名:类型,...):返回类型{
    ......
}

比如

fun sum(a: Int, b: Int): Int {
    return a + b
}

如果不需要返回值,则可以

fun printSum(a: Int, b: Int) {
    println("sum of $a and $b is ${a + b}")
}


2,定义变量

如果是只读变量,用val声明,如果是可修改的变量,用var声明

val a: Int = 1  
val b = 2   // 自动推断类型`Int` 
val c: Int  // 如果没有初始值,则需要提供类型
c = 3       // 稍后赋值
var x = 5 // 自动推断类型`Int`
x += 1

3,字符串模板

var a = 1
// simple name in template:
val s1 = "a is $a" 

a = 2
// arbitrary expression in template:
val s2 = "${s1.replace("is", "was")}, but now is $a"

4,if表达式

fun maxOf(a: Int, b: Int) = if (a > b) a else b

5,对于可能为null的值,必须判断

fun parseInt(str: String): Int? {
    // 如果不是int,就返回null
}

fun printProduct(arg1: String, arg2: String) {
    val x = parseInt(arg1)
    val y = parseInt(arg2)

    // Using `x * y` yields error because they may hold nulls.
    if (x != null && y != null) {
        // x and y are automatically cast to non-nullable after null check
        println(x * y)
    }
    else {
        println("either '$arg1' or '$arg2' is not a number")
    }    
}

6, 用is 关键字判断对象类型,相当于java的instanceOf

fun getStringLength(obj: Any): Int? {
    if (obj !is String) return null

    // `obj` is automatically cast to `String` in this branch
    return obj.length
}

7, list遍历

val items = listOf("apple", "banana", "kiwifruit")
for (item in items) {
    println(item)
}

val items = listOf("apple", "banana", "kiwifruit")
var index = 0
while (index < items.size) {
    println("item at $index is ${items[index]}")
    index++
}
fun describe(obj: Any): String =
    when (obj) {
        1          -> "One"
        "Hello"    -> "Greeting"
        is Long    -> "Long"
        !is String -> "Not a string"
        else       -> "Unknown"
    }

8, 范围

val x = 10
val y = 9
if (x in 1..y+1) {
    println("fits in range")
}

val list = listOf("a", "b", "c")

if (-1 !in 0..list.lastIndex) {
    println("-1 is out of range")
}
if (list.size !in list.indices) {
    println("list size is out of valid list indices range, too")
}
//遍历
for (x in 1..5) {
    print(x)
}
//步长
for (x in 1..10 step 2) {
    print(x)
}
println()
for (x in 9 downTo 0 step 3) {
    print(x)
}

9,集合

for (item in items) {
    println(item)
}

when {
    "orange" in items -> println("juicy")
    "apple" in items -> println("apple is fine too")
}
//lambda表达式
val fruits = listOf("banana", "avocado", "apple", "kiwifruit")
fruits
  .filter { it.startsWith("a") }
  .sortedBy { it }
  .map { it.toUpperCase() }
  .forEach { println(it) }

10,创建对象

val rectangle = Rectangle(5.0, 2.0) //不需要'new'
val triangle = Triangle(3.0, 4.0, 5.0)


参考文献: https://kotlinlang.org/docs/reference/coding-conventions.html

相关内容

热门资讯

【今日要闻】“欢喜麻将开挂神器... 网上科普关于“欢喜麻将有没有挂”话题很是火热,小编也是针对欢喜麻将作*弊开挂的方法以及开挂对应的知识...
【第一消息】“科乐填大坑到底是... 【第一消息】“科乐填大坑到底是不是挂?”(原来真的有挂)您好,科乐填大坑这个游戏其实有挂的,确实是有...
最新引进“新道游炸/金/花开挂... 您好:新道游炸/金/花这款游戏可以开挂,确实是有挂的,需要了解加客服微信【4282891】很多玩家在...
最新引进“趣友怎么装挂?”(原... 最新引进“趣友怎么装挂?”(原来真的有挂)您好,趣友这个游戏其实有挂的,确实是有挂的,需要了解加客服...
重磅消息“大头十三水到底有挂吗... 您好:大头十三水这款游戏可以开挂,确实是有挂的,需要了解加客服微信【9784099】很多玩家在这款游...
今日重大消息“新西游牛牛开挂神... 今日重大消息“新西游牛牛开挂神器?”(确实真的有挂)您好,新西游牛牛这个游戏其实有挂的,确实是有挂的...
终于了解“麻友圈2到底有挂吗?... 终于了解“麻友圈2到底有挂吗?”(原来真的有挂)您好,麻友圈2这个游戏其实有挂的,确实是有挂的,需要...
最新引进“九线拉王可以开挂吗?... 最新引进“九线拉王可以开挂吗?”(太坑了果然有挂)您好,九线拉王这个游戏其实有挂的,确实是有挂的,需...
今日重磅消息“战神牛牛辅助器?... 有 亲,根据资深记者爆料战神牛牛是可以开挂的,确实有挂(咨询软件无需打开...
【今日要闻】“微信红包可以开挂... 【今日要闻】“微信红包可以开挂吗?”(外卦神器下载)您好,微信红包这个游戏其实有挂的,确实是有挂的,...