Kotlin学习(1)概述
来源:互联网 发布:软件行业目标市场分析 编辑:程序博客网 时间:2024/04/30 06:30
变量和常量
//String类型,自动判断类型(不可变变量Value)val laugh = "lalalalala"//浮点类型,默认Doubleval PI = 3.1415//Float类型val PI_FLOAT = 3.1415F//一旦赋值后不能改变,但是可以条件赋值val msg: Int = min(2,1)//可变类型(Variable)var status = 0fun block(){ //但类型不能改变,以下语句报错 //status = "aaa"}
函数
//参数和返回值都是写在: 后面fun max(a: Int, b: Int): Int { //if...else可代替?: return if (a > b) a else b}//函数也不用写在里面//短函数块,还是能用=fun min(a: Int, b: Int): Int = if (a > b) b else a
类的定义
/**class Test(val name: String = "Test")==>等价于public class Person{ private final String name; public Person(String name){ this.name = name; } public String getName(){ return name; }}//val只有getter方法//var有getter和setter方法 *///这是个默认构造器,并且可以赋个默认值class Test(val name: String = "Test") { //参数和返回值都是写在: 后面 fun max(a: Int, b: Int): Int { //if...else可代替?: return if (a > b) a else b }}class Rectangle(val height: Int, val width: Int){ //自定义getter方法 val isSquare: Boolean get() = height == width}fun createRec(): Rectangle{ val random = Random() return Rectangle(random.nextInt(),random.nextInt())}
main函数和模板字符串
//main函数已经不能写在Class里面了fun main(args: Array<String>) { val max = Test().max(1,2) //和Java类似 //字符串模板 println("The max is $max") println("The min is ${min(1,2)}") println(max is Int) //类似于instanceof}
继承和接口
interface Exprclass Num(val value: Int) : Exprclass Sum(val left: Expr, val right: Expr) : Exprfun eval(e: Expr): Int{ if (e is Num){ //此处不用显性类型转化 val n = e //as Num return n.value } if (e is Sum){ //在e is Sum为True时,该作用域内自动转化 return eval(e.left) + eval(e.right) } throw IllegalArgumentException()}fun eval2(e: Expr): Int = when (e){ is Num -> { println("num: ${e.value}") e.value } is Sum -> { val left = eval2(e.left) val right = eval2(e.right) println("sum: $left + $right") left + right } else -> throw IllegalArgumentException() }fun main(args: Array<String>){ val a = eval2(Sum(Sum(Num(1),Num(2)),Num(4))) println(a)}
Enum和when
//枚举类enum class Color ( val r: Int, val g: Int, val b: Int){ RED(255,0,0), ORANGE(255,165,0), GREEN(0,255,0), YELLOW(0,0,255), BLUE(0,0,255), JAOSDHO(161,18,25); fun rgb() = (r * 256 + g) * 256 + b //when来代替switch fun getMnemonic(color: Color) = when (color){ BLUE -> "Blue" GREEN -> "Green" ORANGE,RED -> "Red or Orange" else -> "fuck color" } fun mixColor(c1: Color, c2: Color) = when (setOf<Color>(c1, c2)){ setOf(RED,YELLOW) -> ORANGE setOf(YELLOW,BLUE) -> GREEN else -> throw Exception("Unknown Color") } fun maxColor2(c1: Color, c2: Color): Color{ when{ (c1 == RED && c2 == YELLOW) || (c1 == YELLOW && c2 == RED) -> return ORANGE else -> throw Exception("Unknown Color") } }}
迭代和in
可以使用传统的while(){…}或do{…}while()
fun main(args: Array<String>){ //class kotlin.ranges.IntRange val oneToTen = 1..10 //包括10 val oneToSize = 1 until 10 //不包括10 val tenToOne = 10 downTo 1 //包括1 val stepTwo = 1..10 step 2 //1,3,5,7,9 //class kotlin.ranges.CharRange val charIter = 'A'..'F' //test IntRange for (i in stepTwo){ //println(i) } //Map上的迭代 val sortedMap = TreeMap<Char,String>() //test Map for (c in 'A'..'F'){ //可以以Python的方式访问和赋值 sortedMap[c] = Integer.toBinaryString(c.toInt()) } for ((k,v) in sortedMap){ println("$k = $v") } //List上的迭代 val list = arrayListOf("10","11","1001") for ((index,ele) in list.withIndex()){ println("$index : $ele") } //使用in来判断,使用的是Comparable接口的c fun isLetter(c: Char) = c in 'a'..'z' || c in 'A'..'Z' fun isNotDigit(c: Char) = c !in '0'..'9' println("KCK" in "J".."L") //true,K在J,L中间 println("Kotlin" in setOf("Java","Scala")) //false}
try…catch…finally
可以和Java一样的用法
或作为表达式
fun readNumber(read: BufferedReader){ //try{}作为表达式 val number = try { Integer.parseInt(readLine()) }catch (e: NumberFormatException){ return }}
阅读全文
0 0
- Kotlin学习(1)概述
- Kotlin学习(1):Kotlin简介
- Kotlin学习(1)
- Kotlin学习篇(1)
- kotlin 学习笔记(1)
- Kotlin基础学习(1)
- Kotlin学习总结(1)——Kotlin快速入门
- Kotlin学习(学习kotlin的日子)
- Kotlin学习(一)Kotlin插件安装
- Kotlin学习(二)Hello Kotlin
- kotlin 学习记录 基本语法(1)
- Kotlin 学习笔记(1)基本语法
- kotlin学习1(环境搭建)
- Kotlin学习笔记(1)- 环境配置
- Kotlin语言学习笔记(1)
- Kotlin学习笔记(1):标签
- Kotlin 学习笔记(开始1)
- kotlin学习笔记1
- 用S3Browser对S3做数据同步
- java中Collection接口&List接口
- webpack打包技术
- 一、评价指标
- MFC多线程编程做时间显示程序
- Kotlin学习(1)概述
- STM32获取DHT11温度传感器数据
- 【Data Structure/Algorithm】排序之冒泡排序
- 再起航,我的学习笔记之JavaScript设计模式25(迭代器模式)
- Codeforces Round #435 (Div. 2)C. Mahmoud and Ehab and the xor(异或的性质)
- 【bzoj 1566】管道取珠(DP)
- Python numpy函数:linspace()创建等差数列
- 为什么在vue的组件中,data要用function返回对象呢?
- 【JavaScript】4.面试题一JS基础知识(上)