首页 > 开发 > 其他 > 正文

分分钟学会Kotlin语言(Learn kotlin in Y Minutes)

2019-10-20 15:57:35
字体:
来源:转载
供稿:网友
谷歌宣布kotlin成安卓开发一级语言以后,kotlin大热,Kotlin是JVM上的静态类型的编程语言,它是100%兼容的java。

废话少说,上代码,由于代码太长,分两段:

  1. //这是单行注释 // 
  2. /* 
  3. 这是多行注释 
  4. */ 
  5. // 这和Java的 "package" 差不多. 
  6. package com.learnxinyminutes.kotlin 
  7.  
  8. /* 
  9. Kotlin程序入口函数是名为"main". 
  10. 这个函数的入参是包含命令行参数的数组 
  11. */ 
  12. fun main(args: Array<String>) { 
  13.     /* 
  14.     声明值是使用"var" 或 "val". 
  15.     "val"是一旦分配值后就不能变,不变量,相反"vars" 是声明变量 
  16.     */ 
  17.     val fooVal = 10 // 之后就不能再给fooVal分配其他值了 
  18.     var fooVar = 10 
  19.     fooVar = 20 // fooVar是变量,能够再次分配值 
  20.  
  21.     /* 
  22.     大多数情况, Kotlin能够决定一个变量的类型是什么。 
  23.     这样我们不必显式明确每次去指定变量类型了。 
  24.     我们可以通过如下方式明确定义变量类型: 
  25.     */ 
  26.     val foo : Int = 7 
  27.  
  28.     /* 
  29.     Kotlin包含两种类型的字符串:与Java字符串相同的escaped strings和新的raw strings, 
  30.     escaped strings中的换行符是/n字符串和Java中一样定义, 
  31.     反斜杠是换行符 
  32.     */ 
  33.     val fooString = "My String Is Here!" 
  34.     val barString = "Printing on a new line?/nNo Problem!" 
  35.     val bazString = "Do you want to add a tab?/tNo Problem!" 
  36.     println(fooString) 
  37.     println(barString) 
  38.     println(bazString) 
  39.  
  40.     /* 
  41.     行字符串(raw strings)由三个引号分隔 ("""). 
  42.     顾名思义能包含新的一行和其他任何字符。 
  43.    下面是给fooRawString赋值的是一个函数helloWorld,直至第四行的三个引号才表示赋值结束 
  44.     */ 
  45.     val fooRawString = ""
  46. fun helloWorld(val name : String) { 
  47.    println("Hello, world!"
  48. ""
  49.     println(fooRawString) 
  50.  
  51.     /* 
  52.     字符串能保护模板表达式 
  53.     模板表达式是以美元符号开始($). 
  54.     */ 
  55.     val fooTemplateString = "$fooString has ${fooString.length} characters" 
  56.     println(fooTemplateString) 
  57.  
  58.     /* 
  59.     对于一个变量是空必须明确指定其是nullable. 
  60.     变量可以在其类型后面追加问号?指定其是nullable, 
  61.     然后使用?.操作符访问这个nullable的变量 
  62.     也可以使用?: 操作符在其为null时为其分配一种值。 
  63.     */ 
  64.     var fooNullable: String? = "abc" 
  65.     println(fooNullable?.length) // => 3 
  66.     println(fooNullable?.length ?: -1) // => 3 
  67.     fooNullable = null 
  68.     println(fooNullable?.length) // => null 
  69.     println(fooNullable?.length ?: -1) // => -1 
  70.  
  71.     /* 
  72.     函数是使用"fun" 定义 
  73.     函数参数在函数名后面的括号中指定。函数参数可以有默认值。如果需要,函数返回类型在参数之后指定。 
  74.     */ 
  75.     fun hello(name: String = "world"): String { 
  76.         return "Hello, $name!" 
  77.     } 
  78.     println(hello("foo")) // => Hello, foo! 
  79.     println(hello(name = "bar")) // => Hello, bar! 
  80.     println(hello()) // => Hello, world! 
  81.  
  82.     /* 
  83.     一个函数参数可以使用"vararg" 标注, 
  84.     能够让许多参数传递进入函数。 
  85.     */ 
  86.     fun varargExample(vararg names: Int) { 
  87.         println("Argument has ${names.size} elements"
  88.     } 
  89.     varargExample() // => Argument has 0 elements 
  90.     varargExample(1) // => Argument has 1 elements 
  91.     varargExample(1, 2, 3) // => Argument has 3 elements 
  92.  
  93.     /* 
  94.     当一个函数由单个表达式组成时,那么这个卷括号可以被省略。函数内容体是在等于号=之后指定。 
  95.     */ 
  96.     fun odd(x: Int): Boolean = x % 2 == 1 
  97.     println(odd(6)) // => false 
  98.     println(odd(7)) // => true 
  99.  
  100.     // 如果返回类型能被推断,我们就不必指定了。 
  101.     fun even(x: Int) = x % 2 == 0 
  102.     println(even(6)) // => true 
  103.     println(even(7)) // => false 
  104.  
  105.     // 函数能用函数作为参数,返回页首函数。 
  106.     fun not(f: (Int) -> Boolean): (Int) -> Boolean { 
  107.         return {n -> !f.invoke(n)} 
  108.     } 
  109.     // Named函数能使用 :: 作为参数指定. 
  110.     val notOdd = not(::odd) 
  111.     val notEven = not(::even) 
  112.     // Lambda 表达式能指定为参数 
  113.     val notZero = not {n -> n == 0} 
  114.     /* 
  115.     如果一个lambda仅有一个参数,它的声明可以被忽视(随着 ->一起). 
  116.     单个参数名将是"it". 
  117.     */ 
  118.     val notPositive = not {it > 0} 
  119.     for (i in 0..4) { 
  120.         println("${notOdd(i)} ${notEven(i)} ${notZero(i)} ${notPositive(i)}"
  121.     } 
  122.  
  123.     //  "class" 是用来声明类 
  124.     class ExampleClass(val x: Int) { 
  125.         fun memberFunction(y: Int): Int { 
  126.             return x + y 
  127.         } 
  128.  
  129.         infix fun infixMemberFunction(y: Int): Int { 
  130.             return x * y 
  131.         } 
  132.     } 
  133.     /* 
  134.     创建新的实例使用构造器 
  135.     Kotlin没有 "new"  
  136.     */ 
  137.     val fooExampleClass = ExampleClass(7) 
  138.     // 成员函数能使用点符号调用 
  139.     println(fooExampleClass.memberFunction(4)) // => 11 
  140.  
  141.     /* 
  142.     如果一个函数被"infix"标注,能使用infix notation调用 
  143.     下面fooExampleClass infixMemberFunction 4等同于 
  144. fooExampleClass.memberFunction(4) 
  145.     */ 
  146.     println(fooExampleClass infixMemberFunction 4) // => 28 
  147.  
  148.     /* 
  149.    数据类是创建只包含数据的精确方式. 
  150.      "hashCode"/"equals" 和 "toString" 等方法会自动产生。 
  151.     */ 
  152.     data class DataClassExample (val x: Int, val y: Int, val z: Int) 
  153.     val fooData = DataClassExample(1, 2, 4) 
  154.     println(fooData) // => DataClassExample(x=1, y=2, z=4) 
  155.  
  156.     // 数据类没有 "copy" 函数 
  157.     val fooCopy = fooData.copy(y = 100) 
  158.     println(fooCopy) // => DataClassExample(x=1, y=100, z=4) 
  159.  
  160.     // 对象能被解构入多个变量。 
  161.     val (a, b, c) = fooCopy 
  162.     println("$a $b $c"// => 1 100 4 
  163.  
  164.     // 解构 "for" 循环 
  165.     for ((a, b, c) in listOf(fooData)) { 
  166.         println("$a $b $c"// => 1 100 4 
  167.     } 
  168.  
  169.     val mapData = mapOf("a" to 1, "b" to 2) 
  170.     // Map.Entry 也能解构 
  171.     for ((key, value) in mapData) { 
  172.         println("$key -> $value"
  173.     } 
  174.  
  175.     //  "with" 函数类似JavaScript "with" 语句. 
  176.     data class MutableDataClassExample (var x: Int, var y: Int, var z: Int) 
  177.     val fooMutableData = MutableDataClassExample(7, 4, 9) 
  178.     with (fooMutableData) { 
  179.         x -= 2 
  180.         y += 2 
  181.         z-- 
  182.     } 
  183.     println(fooMutableData) // => MutableDataClassExample(x=5, y=6, z=8) 
  184.  
  185.     /* 
  186.     使用 "listOf" 函数创建集合 
  187.     集合是不可变的,元素不能增加和移走 
  188.     */ 
  189.     val fooList = listOf("a""b""c"
  190.     println(fooList.size) // => 3 
  191.     println(fooList.first()) // => a 
  192.     println(fooList.last()) // => c 
  193.     // list元素可通过索引访问。 
  194.     println(fooList[1]) // => b 
  195.  
  196.     // 一个可变的 list能使用 "mutableListOf" 函数创建 
  197.     val fooMutableList = mutableListOf("a""b""c"
  198.     fooMutableList.add("d"
  199.     println(fooMutableList.last()) // => d 
  200.     println(fooMutableList.size) // => 4 
  201.  
  202.     // 使用 "setOf"  创建set 
  203.     val fooSet = setOf("a""b""c"
  204.     println(fooSet.contains("a")) // => true 
  205.     println(fooSet.contains("z")) // => false 
  206.  
  207.     // 使用 "mapOf" f创建map 
  208.     val fooMap = mapOf("a" to 8, "b" to 7, "c" to 9) 
  209.     // Map values can be accessed by their key. 
  210.     println(fooMap["a"]) // => 8 
  211.  
  212.      //待续 
  213. /* 
  214.     使用 "generateSequence" f函数创建序列号,它是懒赋值的集合,只有使用时才真正赋值。 
  215.     */ 
  216.     val fooSequence = generateSequence(1, { it + 1 }) 
  217.     val x = fooSequence.take(10).toList() 
  218.     println(x) // => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 
  219.  
  220.     // 产生 斐波纳契案例 
  221.     fun fibonacciSequence(): Sequence<Long> { 
  222.         var a = 0L 
  223.         var b = 1L 
  224.  
  225.         fun next(): Long { 
  226.             val result = a + b 
  227.             a = b 
  228.             b = result 
  229.             return a 
  230.         } 
  231.  
  232.         return generateSequence(::next) 
  233.     } 
  234.     val y = fibonacciSequence().take(10).toList() 
  235.     println(y) // => [1, 1, 2, 3, 5, 8, 13, 21, 34, 55] 
  236.  
  237.     // Kotlin提供colletion的高阶函数。 
  238.     val z = (1..9).map {it * 3} 
  239.                   .filter {it < 20} 
  240.                   .groupBy {it % 2 == 0} 
  241.                   .mapKeys {if (it.key) "even" else "odd"
  242.     println(z) // => {odd=[3, 9, 15], even=[6, 12, 18]} 
  243.  
  244.     // "for" 循环 
  245.     for (c in "hello") { 
  246.         println(c) 
  247.     } 
  248.  
  249.     // "while" 循环是和其他语言一样 
  250.     var ctr = 0 
  251.     while (ctr < 5) { 
  252.         println(ctr) 
  253.         ctr++ 
  254.     } 
  255.     do { 
  256.         println(ctr) 
  257.         ctr++ 
  258.     } while (ctr < 10) 
  259.  
  260.     /* 
  261.     "if" 能表达返回一个值的表达式 
  262.     */ 
  263.     val num = 5 
  264.     val message = if (num % 2 == 0) "even" else "odd" 
  265.     println("$num is $message"// => 5 is odd 
  266.  
  267.     // "when" 能作为"if-else if" 替换 
  268.     val i = 10 
  269.     when { 
  270.         i < 7 -> println("first block"
  271.         fooString.startsWith("hello") -> println("second block"
  272.         else -> println("else block"
  273.     } 
  274.  
  275.     // "when" 能带一个参数 
  276.     when (i) { 
  277.         0, 21 -> println("0 or 21"
  278.         in 1..20 -> println("in the range 1 to 20"
  279.         else -> println("none of the above"
  280.     } 
  281.  
  282.     // "when" 能用在返回一个值的函数can be used as a function that returns a value. 
  283.     var result = when (i) { 
  284.         0, 21 -> "0 or 21" 
  285.         in 1..20 -> "in the range 1 to 20" 
  286.         else -> "none of the above" 
  287.     } 
  288.     println(result) 
  289.  
  290.     /* 
  291.     使用"is" 操作符检查类型,如果一个对象传入类型检查,就无需再次明确casting 它了 
  292.     */ 
  293.     fun smartCastExample(x: Any) : Boolean { 
  294.         if (x is Boolean) { 
  295.             // x is automatically cast to Boolean 
  296.             return x 
  297.         } else if (x is Int) { 
  298.             // x is automatically cast to Int 
  299.             return x > 0 
  300.         } else if (x is String) { 
  301.             // x is automatically cast to String 
  302.             return x.isNotEmpty() 
  303.         } else { 
  304.             return false 
  305.         } 
  306.     } 
  307.     println(smartCastExample("Hello, world!")) // => true 
  308.     println(smartCastExample("")) // => false 
  309.     println(smartCastExample(5)) // => true 
  310.     println(smartCastExample(0)) // => false 
  311.     println(smartCastExample(true)) // => true 
  312.  
  313.     // Smartcast also works with when block 
  314.     fun smartCastWhenExample(x: Any) = when (x) { 
  315.         is Boolean -> x 
  316.         is Int -> x > 0 
  317.         is String -> x.isNotEmpty() 
  318.         else -> false 
  319.     } 
  320.  
  321.     /* 
  322.     Extensions是一个增加新函数到类的方式,类似 C# extension 方法 
  323.     */ 
  324.     fun String.remove(c: Char): String { 
  325.         return this.filter {it != c} 
  326.     } 
  327.     println("Hello, world!".remove('l')) // => Heo, word! 
  328.  
  329.     println(EnumExample.A) // => A 
  330.     println(ObjectExample.hello()) // => hello 
  331.  
  332. // Enum类似Java enum类型 
  333. enum class EnumExample { 
  334.     A, B, C 
  335.  
  336. /* 
  337. "object"能被用于创建单例对象,不能初始化它,但是能使用名称引向它。类似 Scala singleton objects. 
  338. */ 
  339. object ObjectExample { 
  340.     fun hello(): String { 
  341.         return "hello" 
  342.     } 
  343.  
  344. fun useObject() { 
  345.     ObjectExample.hello() 
  346.     val someRef: Any = ObjectExample // we use objects name just as is 


注:相关教程知识阅读请移步到kotlin教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表