Kotlin基础教程-风格

来源:互联网 发布:metasploit和数据库 编辑:程序博客网 时间:2024/06/05 17:09

Idioms

DTO

使用Data标识一个类为数据处理类。自带如下方法:

  • getter/setter
  • toString
  • hashCode
  • copy
data class Customer(val name: String, val email: String)fun main(args: Array<String>) {    val customer = Customer("doctorq","542113578@qq.com")    println(customer.toString())    println(customer.equals("dd"))    println(customer.email)    println(customer.name)    println(customer.copy())}

这里写图片描述

函数参数默认值

就是定义函数传参的时候附上初始值就行了

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

上面的函数如果a不传递值是,默认为0。
如果不传a的值,需要显式赋值给b

fun main(args: Array<String>) {    println(maxOf1(b=1))    println(maxOf1(3,2))}fun maxOf1(a: Int = 0, b: Int) = if (a > b) a else b

这里写图片描述

过滤list

val list = listOf(1, 2, 3, 4, -1)val listbyFilter = list.filter { x -> x > 0 }println(listbyFilter)

这里写图片描述

简写如下:

val list = listOf(1, 2, 3, 4, -1)val listbyFilter = list.filter {it >  0 }println(listbyFilter)

字符串中插入变量

val name = "doctorq"println("Nams $name")

这里写图片描述

实例检查

data class Customer(val name: String, val email: String)fun typeCheck(customer:Customer){    when (customer) {        is Customer -> println("this is Foo")        else   -> println("other")    }}

集合

list

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

map

val map = mapOf("a" to 1, "b" to 2, "c" to 3)for((k,v) in map){    println("$k is $v")}

这里写图片描述

如果想要获取必须使用map["key"],必须使用"",''无法识别,这点有点不合理:

这里写图片描述

范围

包含100

for(i in 0..100)

不包含100

for(i in 0 util 100)

懒值

关键字lazy,第一次使用时才会初始化,而且只会初始化一次

val p: String by lazy {    val o = "helloworld"    o}

这里写图片描述

单例模式

用object定义的对象为单例模式

object Singleton {    val name = "doctorq"}fun main(args:Array<String>){    println(Singleton.name)}

这里写图片描述

方法扩展

对于一些已有类的方法,觉得已有方法不好用,可以进行修改或者扩展

比如我想写一个扩展toString方法,返回的字符串前面全部加上我的名字

fun String.toDoctorq(): String {    return "Doctorq! " + toString()}fun main(args: Array<String>) {    println("hello world".toDoctorq())}

这里写图片描述

非空判断缩写

之前在用java操作一个文件对象时,需要先判断File对象是否存在,然后再操作,而kotlin中直接用?来直接判断,如果存在执行后面代码,不存在直接返回null

fun main(args:Array<String>){    val files = File("Test").listFiles()    println(files?.size) }

这里写图片描述

但是返回null这个关键字不够友好,也可以自定义返回值

这里写图片描述

异常处理

fun test() {    val result = try {        count()    } catch (e: ArithmeticException) {        throw IllegalStateException(e)    }    println(result)}fun count():Int{    return 12}fun main(args:Array<String>){    test()}

if表达式赋值给变量

fun foo(param: Int) {    val result = if (param == 1) {        "one"    } else if (param == 2) {        "two"    } else {        "three"    }    println(result)}

这里写图片描述

with组织代码块

class Turtle {    fun penDown(){        println("penDown")    }    fun penUp(){        println("penUp")    }    fun turn(degrees: Double){        println("degrees $degrees")    }    fun forward(pixels: Double){        println("forward $pixels")    }}fun main(args: Array<String>) {    val myTurtle = Turtle()    with(myTurtle) {        //draw a 100 pix square        penDown()        for (i in 1..4) {            forward(100.0)            turn(90.0)        }        penUp()    }}

这里写图片描述

stream

fun main(args:Array<String>){    val stream = Files.newInputStream(Paths.get("/Users/doctorq/Documents/Developer/git-project/learningkotlin1/learningkotlin1.iml"))    stream.buffered().reader().use { reader ->        println(reader.readText())    }}

这里写图片描述

可能为空值的布尔类型

fun main(args: Array<String>) {    val a: Boolean? = null    val b: Boolean? = false    val c: Boolean? = true    checkBoolean(a)    checkBoolean(b)    checkBoolean(c)}fun checkBoolean(b: Boolean?) {    if (b == true) {        println("$b")    } else {        println("`b` is false or null")    }}

这里写图片描述

请关注我的微信公众号号,实时接收最新文章

这里写图片描述

原创粉丝点击