All Projects → panxl6 → Kotlin In Action

panxl6 / Kotlin In Action

《kotlin实战》翻译

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to Kotlin In Action

Serverless Sinatra Sample
Demo code for running Ruby Sinatra on AWS Lambda
Stars: ✭ 195 (-19.09%)
Mutual labels:  lambda
Apilogs
Easy logging and debugging for Amazon API Gateway and AWS Lambda Serverless APIs
Stars: ✭ 216 (-10.37%)
Mutual labels:  lambda
Algnhsa
AWS Lambda Go net/http server adapter
Stars: ✭ 226 (-6.22%)
Mutual labels:  lambda
Aws Lambda Power Tuning
AWS Lambda Power Tuning is an open-source tool that can help you visualize and fine-tune the memory/power configuration of Lambda functions. It runs in your own AWS account - powered by AWS Step Functions - and it supports three optimization strategies: cost, speed, and balanced.
Stars: ✭ 3,040 (+1161.41%)
Mutual labels:  lambda
Knative Lambda Runtime
Running AWS Lambda Functions on Knative/Kubernetes Clusters
Stars: ✭ 201 (-16.6%)
Mutual labels:  lambda
Kepubify
Fast, standalone EPUB to KEPUB converter CLI app / library (and a few other utilities).
Stars: ✭ 225 (-6.64%)
Mutual labels:  ebooks
Aws Auto Remediate
Open source application to instantly remediate common security issues through the use of AWS Config
Stars: ✭ 191 (-20.75%)
Mutual labels:  lambda
Now Deno
Deno builder for Vercel - run Deno on Vercel. :sauropod: + λ = ❤️
Stars: ✭ 238 (-1.24%)
Mutual labels:  lambda
Serverless Slack App
A Serverless.js Slack App Boilerplate with OAuth and Bot actions
Stars: ✭ 217 (-9.96%)
Mutual labels:  lambda
Dapper.lnskydb
基于Dapper的LINQ扩展,支持Lambda表达式,支持按时间分库分表,也可以自定义分库分表方法,且实体类有T4模版自动生成.省去手写实体类的麻烦。已在实际项目使用
Stars: ✭ 228 (-5.39%)
Mutual labels:  lambda
Aws Mobile React Native Starter
AWS Mobile React Native Starter App https://aws.amazon.com/mobile
Stars: ✭ 2,247 (+832.37%)
Mutual labels:  lambda
Obscurify
Find out more about your music taste and compare it to others' with Obscurify
Stars: ✭ 200 (-17.01%)
Mutual labels:  lambda
Streamalert
StreamAlert is a serverless, realtime data analysis framework which empowers you to ingest, analyze, and alert on data from any environment, using datasources and alerting logic you define.
Stars: ✭ 2,634 (+992.95%)
Mutual labels:  lambda
Lambdapp
Anonymous functions in C
Stars: ✭ 195 (-19.09%)
Mutual labels:  lambda
Komiser
☁️ Cloud Environment Inspector 👮🔒 💰
Stars: ✭ 2,684 (+1013.69%)
Mutual labels:  lambda
React On Lambda
A JavaScript library for building React applications in more functional way. Alternative to JSX.
Stars: ✭ 192 (-20.33%)
Mutual labels:  lambda
Serverless Analytics
Track website visitors with Serverless Analytics using Kinesis, Lambda, and TypeScript.
Stars: ✭ 219 (-9.13%)
Mutual labels:  lambda
Mercury Parser Api
🚀 A drop-in replacement for the Mercury Parser API.
Stars: ✭ 239 (-0.83%)
Mutual labels:  lambda
Bless
Repository for BLESS, an SSH Certificate Authority that runs as a AWS Lambda function
Stars: ✭ 2,627 (+990.04%)
Mutual labels:  lambda
Serverless Chrome
🌐 Run headless Chrome/Chromium on AWS Lambda
Stars: ✭ 2,625 (+989.21%)
Mutual labels:  lambda

Google正式支持Kotlin啦~

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

Maintenance

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 // 编译通过

var b: String = "abc"
b = 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

扩展函数

扩展函数是定义在类外部,但却能够以类的成员函数的方式进行调用的函数。

package strings

fun String.lastChar(): Char = this.get(this.length - 1)

控制结构

// 定义一个类
class Person(val name: String)

// 定义类属性
class Person(
  val name: String,
  var isMarried: Boolean
)

// 定义类方法
class Person(
  val name: String,
  var isMarried: Boolean
) {
  fun sayHello() {
    println("hello $name")
  }
}

// 创建类实例
>>> val person = Person("Bob", true)
>>> println(person.name)
Bob
>>> person.sayHello()
hello BOb

类的访问修饰符

修饰符 对应的成员 备注
final 不能被覆盖 类成员的默认修饰符
open 可以被覆盖 必须显式的指定
abstract 必须被覆盖 只能在抽象类中使用,抽象成员不能有实现
override 在一个子类中覆盖一个成员 如果没有被标记为final,覆盖的成员默认是开放的。

类的可见性修饰符

修饰符 对应的成员 顶层声明
public(默认可见性) 所有地方可见 所有地方可见
internal 模块内可见 模块内可见
protected 子类内部可见 不可见
private 类内部可见 在文件中可见

枚举类

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

接口

// 定义接口
interface Clickable {
  fun click()
  fun showOff() = println("I'm clickable!") // 接口可以有默认的实现
}

// 实现接口
class Button : Clickable {
  override fun click() = println("I was clicked")
}

对象声明

// 定义对象
object Payroll {
  val allEmployees = arrayListOf<Person>()
  
  fun calculateSalary() {
    for (person in allEmployees) {
      ...
    }
  }
}

// 使用对象
Payroll.allEmployees.add(Person(...))
Payroll.calculateSalary()

伴生对象

// 定义伴生对象
class A {
  companion object {
    fun bar() {
      println("Companion object called")
    }
  }
}

// 调用伴生对象
>>> A.bar()
Companion object called

Lambda表达式

>>> val sum = { x: Int, y: Int -> x + y } // 定义lambda表达式
>>> println(sum(1, 2))
3

lambda syntax

类型系统

操作符重载

标注

反射

泛型

traits

集合

异常处理

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

文件操作

函数式支持

如果你对函数式编程不是特别了解,可以看一看《函数式编程思维》

filter

>>> val list = listOf(1, 2, 3, 4)
>>> list.filter { it % 2 == 0 }
[2, 4]

图5.3

map

>>> val list = listOf(1, 2, 3, 4)
>>> list.map { it * it }
[1, 4, 9, 16]

图5.4

all

val canBeInClub27 = { p: Person -> p.age <= 27 }

>>> val people = listOf(Person("Alice", 27), Person("Bob", 31))
>>> println(people.all(canBeInClub27))
false

any

val canBeInClub27 = { p: Person -> p.age <= 27 }

>>> val people = listOf(Person("Alice", 27), Person("Bob", 31))
>>> println(people.any(canBeInClub27))
true

count

>>> val people = listOf(Person("Alice", 27), Person("Bob", 31))
>>> println(people.count(canBeInClub27))
1

find

>>> val people = listOf(Person("Alice", 27), Person("Bob", 31))
>>> println(people.find(canBeInClub27))
Person(name=Alice, age=27)

领域特定语言

常用函数

IDE快捷键

Kotlin-in-action中文翻译

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


相关资源:

  1. Awesom-kotlin
  2. Belarus Kotlin User Group
  3. Programming in kotlin电子书
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].