• Kotlin基础(一)Kotlin快速入门


    Kotlin快速入门

    一、函数

     1 /*
     2 *  1.函数可以定义在文件最外层,不需要把它放在类中
     3 *  2.可以省略结尾分号
     4 * */
     5 fun main(args: Array<String>) {
     6     println(max2(1,2))
     7     println(max(3,5))
     8 }
     9 
    10 fun max(a: Int, b: Int): Int{
    11     return if (a>b) a else b
    12 }
    13 
    14 /*
    15 *  与Java不同,Kotlin中,除了循环(for、do和do/while)
    16 *  以外大多数控制结构都是表达式,而不是语句(表达式和语句的区别?)。
    17 *  所以可以有如下写法。既然有了值(可推断返回类型),就可以省略返回类型。注意,只有
    18 *  表达式体函数的返回类型可以省略,对于有返回值的代码块体函数不可省略(这么设计的好处?)。
    19 * */
    20 fun max2(a: Int, b: Int)=if (a>b) a else b

    二、变量

     1 fun main(args: Array<String>) {
     2     //声明变量的三种方式
     3     val a="For the Lichking!"
     4     val b: Int=88
     5     val c=8.8e8
     6     val d: String
     7     d="ssss"
     8 
     9    /*
    10    * 声明变量的关键字有两个:
    11    * val(来自value),不可变引用。相当于Java中的final变量。
    12    * var(来自variable),可变引用(不同于Javascript,类型仍然不可变)。
    13    * 相当于Java中的普通变量
    14    * */
    15 
    16 
    17     /*
    18     * 如果编译器能确保只有唯一一条初始化语句被执行,可根据条件使用不同值初始化它。
    19     * */
    20     val message: String
    21     val isTrue: Boolean=true
    22     if (isTrue) message="true" else message="false"
    23 }

    三、字符串模板

    1 fun main(args: Array<String>) {
    2     //只需要在变量名前加"$"就可以在字符串字面量中引用变量
    3     val name=if (args.isNotEmpty()) args[0] else "Kotlin"
    4     println("Hello,${name}")
    5 
    6     //还可以这么玩儿
    7     println("Hello,${if (args.isNotEmpty()) args[0] else "NoJava"}")
    8 }

    四、类

    1.java类

     1 public class Person {
     2     private final String name;
     3 
     4     public Person(String name){
     5         this.name=name;
     6     }
     7     
     8     public String getName() {
     9         return name;
    10     }
    11 }

    转换为Kotlin类

    1 //这种只有数据没有其他代码的类,通常被叫做值对象
    2 class Person(val name: String)

    属性:

     1 /*
     2 *  在Java中,字段和其访问器的组合叫做属性。
     3 *  Kotlin的类中声明一个变量就是声明一个属性。
     4 * */
     5 class Man(
     6         val name:String, //只读属性:生成字段和其getter
     7         var isMarried:Boolean) //可写属性:生成字段和getter、setter
     8 
     9 class Rectangle(val height: Int,val  Int){
    10     val isSquare: Boolean
    11     //自定义访问器
    12     /*get() {
    13         return height==width
    14     }*/
    15 
    16     get() = height==width
    17 }
    18 
    19 fun main(args: Array<String>) {
    20     val tang=Man("Tang Jiujia",false)
    21     println("My name is ${tang.name}")
    22 
    23     val rectangle=Rectangle(22,33)
    24     println(rectangle.isSquare)
    25 }

    五、目录和包

     1 package gemetry.shapes
     2 
     3 import java.util.*
     4 
     5 class Rectangle(val height:Int,val  Int){
     6     val isSquare: Boolean
     7         get() = height==width
     8 }
     9 
    10 //顶层函数
    11 fun createRectangle(): Rectangle{
    12     val random = Random()
    13     return Rectangle(random.nextInt(),random.nextInt())
    14 }
    15 
    16 //Kotlin中,可以把多个类放在同一个文件中,文件的名字还可以随意取
    17 //因此可以不创建shapes文件夹,直接用shapes文件代替文件夹。
    18 //当然,坚持Java的目录文件结构仍然是个不错的选择
    19 class RectangleUtil()
    1 package gemetry.example
    2 
    3 //可以这样导入顶层函数,当然使用"*"时顶层函数也会被导入
    4 import gemetry.shapes.createRectangle
    5 
    6 fun main(args: Array<String>) {
    7     println("Height: ${createRectangle().height}"+"
    "+"Width: ${createRectangle().width}")
    8 }

    六、表示和处理选择:枚举和when

     1 package gemetry.enums
     2 
     3 enum class Color(val r: Int,val g: Int,val b:Int){
     4     RED(255,0,0),ORANGE(255,165,0),YELLOW(255,255,0),
     5     GREEN(0,255,0),BLUE(0,0,255); //这里分号不能省略,如果要在枚举类
     6     //中定义任何方法,必须用分号把常量列表和方法分开。
     7 
     8     //同Java一样,可以在枚举类中声明属性和方法
     9     fun rgb()=(r*256+g)*256+b
    10 }
    11 
    12 fun getMc(color: Color)=
    13         when (color){
    14             Color.BLUE -> "blue"
    15             //不写"break"
    16             //可以多个情况一起
    17             Color.GREEN,Color.RED -> "green or red"
    18             //必须把所有的可能的情况都列出来,不然就用"else"
    19             Color.YELLOW,Color.ORANGE ->"others"
    20         }
    21 
    22 fun mix(c1: Color,c2: Color)=
    23         //与Java中的switch只能使用常量(枚举常量、字符串、数字字面量)
    24         //不同,when允许使用任何对象,任何表达式
    25         when (setOf<Color>(c1,c2)){
    26             setOf(Color.RED,Color.YELLOW) -> "orange"
    27             setOf(Color.YELLOW,Color.BLUE) ->"GREEN"
    28             setOf(Color.BLUE,Color.RED) -> "unknow"
    29             else -> "???"
    30         }
    31 
    32 fun mix2(c1: Color,c2: Color)=
    33         //不带参数的when
    34         when{
    35             (c2==Color.RED && c2==Color.YELLOW) ||
    36                     (c1==Color.YELLOW && c2==Color.RED) -> "orange"
    37             else -> "others"
    38         }
    39 
    40 fun main(args: Array<String>) {
    41     println(getMc(Color.ORANGE))
    42     println(mix(Color.YELLOW,Color.BLUE))
    43     println(mix2(Color.RED,Color.GREEN))
    44 }

     七、智能转换和“代码块最后表达式就是结果”

     1 package gemetry
     2 
     3 
     4 interface Expr
     5 class Num(val value: Int) : Expr //实现了Expr
     6 class Sum(val left: Expr,val right: Expr) : Expr
     7 /*
     8 fun eval(e: Expr) : Int=
     9         if (e is Num) e.value
    10         else if (e is Sum) eval(e.right)+ eval(e.left)
    11         else throw IllegalArgumentException("Unknown expression")*/
    12 
    13 fun eval(e: Expr): Int=
    14         //when表达式不仅限于检查值是否相等,也可用于检测when实参类型。
    15         when(e){
    16             is Num -> e.value //"is" 判断后自动转换为该类型
    17         // (被判断的属性必须是一个val属性,而且不能有自定义访问器)
    18             is Sum -> eval(e.right) + eval(e.left)
    19             else -> throw IllegalArgumentException("Unknown expression")
    20         }
    21 
    22 fun evalWithLogging(e: Expr) : Int=
    23         when(e){
    24             is Num -> {
    25                 println("num: ${e.value}")
    26                 e.value //代码快的最后一个表达式就是结果
    27                 /*
    28                 * 规则-----“代码块中最后的表达式就是结果”,在所有使用代码块并期望得到一个
    29                 * 结果的地方成立。但是,这个规则对常规函数不成立。一个函数要么具有不是代码
    30                 * 块的表达式函数体,要么具有包含显示return语句的代码块函数体。
    31                 * */
    32             }
    33 
    34             is Sum -> {
    35                 val left= evalWithLogging(e.left)
    36                 val right= evalWithLogging(e.right)
    37                 println("sum: $left+$right")
    38                 left+right
    39             }
    40             else -> {
    41                 throw IllegalArgumentException("Unknown Expression")
    42             }
    43         }
    44 
    45 fun main(args: Array<String>) {
    46     println(eval(Sum(Sum(Num(1),Num(3)),Num(3))))
    47     println(evalWithLogging(Sum(Sum(Num(1),Num(3)),Num(3))))
    48 }

    八、迭代

    1.when 同Java中一样

    2.for: 仅以for-each循环的形式存在 for <item> in <elements>

    3.while:同Java

    4.区间:为了代替Java中的普通for循环。区间本质就是值之间的间隔,这两个值通常是数字:一个起始值,一个结束值。使用..运算符表示。

    能迭其所有值的区间,叫做数列。

    1 val i: Int=0
    2     for (i in 1..10){
    3         println(i)
    4     }
    1 val i: Int=0
    2     //反向迭代,步长为2
    3     for (i in 20 downTo 1 step 2) println(i)

    5.迭代map

     1 fun main(args: Array<String>) {
     2     val binaryReps=TreeMap<Char,String>()
     3 
     4     for (c in 'A'..'F') { //区间迭代字符
     5         val binary=Integer.toBinaryString(c.toInt())
     6         binaryReps[c]=binary //根据键来更新map的简明语法
     7     }
     8     
     9     for ((l,b) in binaryReps) println("$l=$b") //迭代map
    10 }

    6.使用in来检查值是否在区间中

    1 fun isLetter(c: Char)= c in 'a'..'z' || c in 'A'..'Z'
    2 fun isNotDigit(c: Char)=c !in '0'..'9' //不在区间中
    println("Kotlin" in "Java".."Scala")//字符串是按字母顺序表比较
        // (因为String就是这样实现Comparable接口的)

    九、异常

    1.Kotlin并不区分受检异常和非受检异常,因此函数可以不抛出特定异常,异常可以被处理或者不处理。

    2.“try”可以作为一个表达式。

    Simple is important!
  • 相关阅读:
    LTPA Cookie原理
    如何对更改internet密码所需的缓存时间进行调整?
    Freemem eclipse查看内存的小插件
    Java中static、final用法小结
    MANIFEST.MF内容属性名详细解释
    Java程序员的良药:应用程序的开发技巧
    spring struts2 ibatis框架整合开发
    java中静态代码块的用法 static用法详解 类的加载顺序
    从svn上直接导入项目到workspace中
    Eclipse自动生成UML图 Green UML和ModelGoon(直接推拽)
  • 原文地址:https://www.cnblogs.com/Shadowplay/p/7901541.html
Copyright © 2020-2023  润新知