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") }}
请关注我的微信公众号号,实时接收最新文章
阅读全文
0 0
- Kotlin基础教程-风格
- Kotlin基础教程-HelloWorld
- Kotlin基础教程-基础语法
- Kotlin基础教程-基本数据类型
- Kotlin基础教程-包
- Kotlin基础教程-接口
- Kotlin基础教程-扩展
- Kotlin基础教程-数据对象
- Kotlin基础教程-泛型
- Kotlin基础教程-嵌套类
- Kotlin基础教程-枚举类
- Kotlin基础教程-代理
- Kotlin基础教程-代理属性
- Kotlin基础教程-函数
- Kotlin基础教程-内联函数
- Kotlin基础教程-协程
- Kotlin基础教程-Ranges表达式
- Kotlin基础教程-this关键字
- 记一些最近的事
- ASP+PHP 标准sql注入语句(完整版)
- 微博分享网页 简单
- 自媒体如何拥有多个头条号
- WEB安全:XSS漏洞与SQL注入漏洞介绍及解决方案
- Kotlin基础教程-风格
- UVa 11464 Even Parity——思路题
- PCB各层的含义 (solder paste 区别)
- 微信小程序scroll-view高度设置及scroll-into-view的使用跳转
- 关于自定义NodeMCU功能的方法
- java笔记 继承
- 学习Python的优势有哪些
- 关于图像处理的初始总结
- Spring--IoC--基于注解的DI-使用Spring的JUnit4测试