Git Product home page Git Product logo

kotlin-in-action's Introduction

Google正式支持Kotlin啦~

Java 8推广不及预期,Java 9争议颇多,来试试Kotlin吧!

Kotlin语法一览

Hello world

// 最简版
fun main(args: Array<String>) {
  println("Hello, world!")
}
// 面向对象版
class Greeter(val name: String) {
  fun greet() {
    println("Hello, $name")
  }
}

fun main(args: Array<String>) {
  Greeter(args[0]).greet()
}

可变和不可变类型

var的内容来自变量。它可以随时改变。

var answer = 12
answer = 13 // 编译通过

val的内容来自值。初始化之后不能改变。

val age = 18
age = 19 // 编译错误

可为空类型

var a: String? = "abc"
a = null // 编译通过

数据类型

整数类型:val answer: Int = 42 1.1版本开始支持类似swift的下划线分隔: val oneMillion = 1_000_000

浮点类型:val yearsToCompute = 7.5e6

字符类型: val question = "The Ultimate Question of Life, the Universe, and Everything"

输入输出

字符串模板

val $name = 'world'
println("Hello, $name!")

大括号内可以是kotlin表达式

fun main(args: Array<String>) {
  if (args.size > 0) {
    println("Hello, ${args[0]}!")
  }
  
  println("Hello, ${if (args.size > 0) args[0] else "someone"}!")
}

函数

常规定义

fun max(a: Int, b: Int): Int {
  return if (a > b) a else b
}

把函数当成表达式 fun max(a: Int, b: Int): Int = if (a > b) a else b

使用命名参数

fun <T> joinToString(
  collection: Collection<T>,
  separator: String,
  prefix: String,
  postfix: String
): String {
  val result = StringBuilder(prefix)
  
  for ((index, element) in collection.withIndex()) {
    if (index > 0) result.append(separator)
    result.append(element)
  }
  
  result.append(postfix)
  return result.toString()
}

joinToString(collection, separator = " ", prefix = " ", postfix = ".")

使用参数默认值

>>> val list = listOf(1, 2, 3)
>>> println(list)
[1, 2, 3]

fun <T> joinToString(
  collection: Collection<T>,
  separator: String = ", ",
  prefix: String = "",
  postfix: String = ""
): String {
  val result = StringBuilder(prefix)
  
  for ((index, element) in collection.withIndex()) {
    if (index > 0) result.append(separator)
    result.append(element)
  }
  
  result.append(postfix)
  return result.toString()
}

>>> joinToString(list, ", ", "", "")
1, 2, 3
>>> joinToString(list)
1, 2, 3
>>> joinToString(list, "; ")
1; 2; 3

控制结构

接口

traits

扩展函数

集合

异常处理

fun readNumber(reader: BufferedReader): Int? {
  try {
    val line = reader.readLine()
    return Integer.parseInt(line)
  }
  catch (e: NumberFormatException) {
    return null
  }
  finally {
    reader.close()
  }
}

文件操作

函数式支持

领域特定语言

函数式支持

枚举类

enum class Color {
  RED, ORANGE, YELLOW, GREEN, BLUE, INDIGO, VIOLET
}

常用函数

IDE快捷键

Kotlin-in-action

Kotlin in action 一书的中文翻译

更方便的阅读模式请移步gitbook项目

ebook目录下是原文原版电子书和书中源码

亲,如果你觉得这个项目不错,请为我star一下~

ebook store in ebook directory, download or star is welcomed


相关资源:

  1. Awesom-kotlin
  2. Belarus Kotlin User Group
  3. Programming in kotlin电子书

kotlin-in-action's People

Contributors

panxl6 avatar sysupanxl6 avatar

Stargazers

小米`s avatar

Watchers

James Cloos avatar Xiangli Jiaxing avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.