Kotlin 备忘清单是 Kotlin 编程语言的单页参考表
fun main() {
// Code goes here
}
main() 函数是每个 Kotlin 程序的起点,在执行之前必须包含在代码中
println("Greetings, earthling!")
print("Take me to ")
print("your leader.")
/*
打印:
Greetings, earthling!
Take me to your leader.
*/
// 这是单行注释
/*
这个
注释
用于
多
行
*/
fun main() {
println("I will be printed first.")
println("I will be printed second.")
println("I will be printed third.")
}
var age = 25
age = 26
val goldenRatio = 1.618
// 以下变量在双引号内分配了一个文本值
// 因此推断的类型是 String
var color = "Purple"
var streetAddress = "123 Main St."
var cityState = "Brooklyn, NY"
println(streetAddress + " " + cityState)
// 打印: 123 Main St. Brooklyn, NY
var address = "123 Main St."
println("The address is $address")
// 打印: The address is 123 Main St.
var monument = "the Statue of Liberty"
println(monument.capitalize())
// 打印: The Statue of Liberty
println(monument.length)
// 打印: 21
print("\"Excellent!\" I cried. \"Elementary,\" said he.")
// 打印: "Excellent!" I cried. "Elementary," said he.
\n
插入新行\t
插入标签\r
插入回车\'
插入单引号\"
插入双引号\\
插入反斜杠\$
插入美元符号
5 + 7 // 12
9 - 2 // 7
8 * 4 // 32
25 / 5 // 5
31 % 2 // 1
+
加法、-
减法、*
乘法、/
除法和 %
模数
5 + 8 * 2 / 4 - 3 // 6
3 + (4 + 4) / 2 // 7
4 * 2 + 1 * 7 // 15
3 + 18 / 2 * 1 // 12
6 - 3 % 2 + 2 // 7
var batteryPercentage = 80
// 长语法
batteryPercentage = batteryPercantage + 10
// 带有增广赋值运算符的短句法
batteryPercentage += 10
var year = 2019
year++ // 2020
year-- // 2019
Math.pow(2.0, 3.0) // 8.0
Math.min(6, 9) // 6
Math.max(10, 12) // 12
Math.round(13.7) // 14
var morning = true
if (morning) {
println("Rise and shine!")
}
// 打印: Rise and shine!
var rained = false
if (rained) {
println("今天不需要给植物浇水。")
} else {
println("植物需要浇水!")
}
// 打印: 植物需要浇水!
var age = 65
if (age < 18 ) {
println("您被视为未成年人")
} else if (age < 60) {
println("您被视为成年人")
} else {
println("你被认为是高级")
}
// 打印: 你被认为是高级
var myAge = 19
var sisterAge = 11
var cousinAge = 11
myAge > sisterAge // true
myAge < cousinAge // false
myAge >= cousinAge // true
myAge <= sisterAge // false
var humid = true
var raining = true
var jacket = false
println(!humid)
// 打印: false
println(jacket && raining)
// 打印: true
println(humid || raining)
// 打印: true
var humid = true
var raining = true
var shorts = false
var sunny = false
// true AND true
println(humid && raining) // true
// true AND false
println(humid && shorts) // false
// false AND true
println(sunny && raining) // false
// false AND false
println(shorts && sunny) // false
var late = true
var skipBreakfast = true
var underslept = false
var checkEmails = false
// true OR true
println(skipBreakfast || late) // true
// true OR false
println(late || checkEmails) // true
// false OR true
println(underslept || late) // true
// false OR false
println(checkEmails || underslept) // false
var hungry = true
var full = false
println(!hungry) // false
println(!full) // true
!true && (false || true) // false
/*
(false || true) 被评估首先返回 true。
然后,评估 !true && true 并返回最终结果 false
*/
!false && true || false // true
/*
!false 被评估首先返回 true。
然后评估 true && true,返回 true。
那么,真|| 评估 false 最终返回 true
*/
var myAge = 22
var sisterAge = 21
myAge == sisterAge // false
myAge !== sisterAge // true
var studied = true
var wellRested = true
if (wellRested) {
println("祝你今天好运!")
if (studied) {
println("你应该为考试做好准备!")
} else {
println("考试前花几个小时学习!")
}
}
// 打印: 祝你今天好运!
// 打印: 你应该为考试做好准备!
var grade = "A"
when(grade) {
"A" -> println("很棒的工作!")
"B" -> println("做得太好了!")
"C" -> println("你通过了!")
else -> println("关!下次一定要多准备!")
}
// 打印: 很棒的工作!
var height = 46 // inches
if (height in 1..53) {
println("抱歉,您必须至少 54 英寸才能乘坐过山车")
}
// Prints: 抱歉,您必须至少 54 英寸才能乘坐过山车
var programmingLanguages = listOf("C#", "Java", "Kotlin", "Ruby")
var fruits = mutableListOf("Orange", "Apple", "Banana", "Mango")
var cars = listOf("BMW", "Ferrari", "Volvo", "Tesla")
println(cars[2]) // Prints: Volvo
var worldContinents = listOf("Asia", "Africa", "North America", "South America", "Antarctica", "Europe", "Australia")
println(worldContinents.size) // Prints: 7
var seas = listOf("Black Sea", "Caribbean Sea", "North Sea")
println(seas.contains("North Sea")) // Prints: true
// contains() 函数对任何列表执行读取操作并确定元素是否存在
seas.add("Baltic Sea") // 错误:无法对不可变列表执行写操作
// add() 函数只能在可变列表上调用,因此上面的代码会引发错误
var primaryColors = setOf("Red", "Blue", "Yellow")
var womenInTech = mutableSetOf("Ada Lovelace", "Grace Hopper", "Radia Perlman", "Sister Mary Kenneth Keller")
var companies = setOf("Facebook", "Apple", "Netflix", "Google")
println(companies.elementAt(3))
// Prints: Google
println(companies.elementAt(4))
// Returns and Error
println(companies.elementAtOrNull(4))
// Prints: null
var averageTemp = mapOf("winter" to 35, "spring" to 60, "summer" to 85, "fall" to 55)
var europeanDomains = mutableMapOf("Germany" to "de", "Slovakia" to "sk", "Hungary" to "hu", "Norway" to "no")
var oscarWinners = mutableMapOf("Parasite" to "Bong Joon-ho", "Green Book" to "Jim Burke", "The Shape Of Water" to "Guillermo del Toro")
println(oscarWinners.keys)
// Prints: [Parasite, Green Book, The Shape Of Water]
println(oscarWinners.values)
// Prints: [Bong Joon-ho, Jim Burke, Guillermo del Toro]
println(oscarWinners["Parasite"])
// Prints: Bong Joon-ho
var worldCapitals = mutableMapOf("United States" to "Washington D.C.", "Germany" to "Berlin", "Mexico" to "Mexico City", "France" to "Paris")
worldCapitals.put("Brazil", "Brasilia")
println(worldCapitals)
// Prints: {United States=Washington D.C., Germany=Berlin, Mexico=Mexico City, France=Paris, Brazil=Brasilia}
worldCapitals.remove("Germany")
println(worldCapitals)
// Prints: {United States=Washington D.C., Mexico=Mexico City, France=Paris, Brazil=Brasilia}
fun greet() {
println("Hey there!")
}
fun main() {
// Function call
greet() // Prints: Hey there!
}
fun birthday(name: String, age: Int) {
println("Happy birthday $name! You turn $age today!")
}
fun main() {
birthday("Oscar", 26)
// Prints: Happy birthday Oscar! You turn 25 today!
birthday("Amarah", 30)
// Prints: Happy birthday Amarah! You turn 30 today!
}
fun favoriteLanguage(name: String, language: String = "Kotlin") {
println("Hello, $name. Your favorite programming language is $language")
}
fun main() {
favoriteLanguage("Manon")
// Prints: Hello, Manon. Your favorite programming language is Kotlin
favoriteLanguage("Lee", "Java")
// Prints: Hello, Lee. Your favorite programming language is Java
}
fun findMyAge(currentYear: Int, birthYear: Int) {
var myAge = currentYear - birthYear
println("I am $myAge years old.")
}
fun main() {
findMyAge(currentYear = 2020, birthYear = 1995)
// Prints: I am 25 years old.
findMyAge(birthYear = 1920, currentYear = 2020)
// Prints: I am 100 years old.
}
// Return type is declared outside the parentheses
fun getArea(length: Int, width: Int): Int {
var area = length * width
// return statement
return area
}
fun main() {
var myArea = getArea(10, 8)
println("The area is $myArea.")
// Prints: The area is 80.
}
fun fullName(firstName: String, lastName: String) = "$firstName $lastName"
fun main() {
println(fullName("Ariana", "Ortega"))
// Prints: Ariana Ortega
println(fullName("Kai", "Gittens"))
// Prints: Kai Gittens
}
fun main() {
// Anonymous Function:
var getProduct = fun(num1: Int, num2: Int): Int {
return num1 * num2
}
println(getProduct(8, 3))
// Prints: 24
// Lambda Expression
var getDifference = { num1: Int, num2: Int -> num1 - num2 }
println(getDifference(10, 3))
// Prints: 7
}
// 注意啦,这里的 num1AndNum2 有个 operation,它是接收了一个函数作为形参
fun num1AndNum2(num1: Int, num2: Int, operation: (Int, Int) -> Int): Int {
// 让我们试着向 operation 传入参数
return operation(num1, num2)
}
fun plus(num1: Int, num2: Int): Int {
return num1 + num2
}
fun main(args: Array<String>) {
val total = num1AndNum2(100, 200, ::plus)
println(total)//300
// 怎么样?我们利用传入一个函数来充当另一个函数的参数
}
还记得我们怎么在 Java 中用接口吗?试着用函数参数简化它
//operation是一个函数类型的参数哦
fun num1AndNum2(num1: Int, num2: Int, operation: (Int, Int) -> Int): Int {
return operation(num1, num2)
}
fun main(args: Array<String>) {
//这里我们定义一个匿名函数
val operation: (Int, Int) -> Int = { i: Int, i2: Int ->
i + i2
}
val total = num1AndNum2(100, 200, operation)
println(total) //300
}
//我们还是不改变什么
fun num1AndNum2(num1: Int, num2: Int, operation: (Int, Int) -> Int): Int {
return operation(num1, num2)
}
fun main(args: Array<String>) {
//wow哦天哪,Lambda可以做到这样简洁
val total = num1AndNum2(100, 200) { n1, n2 ->
n1 + n2
}
println(total)
}
这里之所以可以把lambda写在外部,是因为operation是最后一个参数。
// Kotlin File
fun String.lettersCount(): Int {
var count = 0
// this 相当于我们下面写的字符串具体的内容
// for 可以用 forEach 代替
for (char in this) {
// 判断是不是字母(包括中文)
if (char.isLetter()) {
count++
}
}
return count
}
fun main() {
//不修改 String 类的情况下新增方法
println("123demo".lettersCount())
// Print: 4
}
class Money(var amount: Double)
// 配合扩展函数,重载运算符 + 即 plus
operator fun Money.plus(money: Money): Money {
// 把金额相加返回一个新的 Money对象
return Money(this.amount + money.amount)
}
fun main() {
val appleMoney = Money(10.0)
val eggMoney = Money(6.0)
// 你没有看错,我们将两个类对象相加了
val allMoney = appleMoney + eggMoney
println(allMoney.amount)
// Print: 16.0
}
这里的 运算符重载 依赖于 扩展函数
// infix 定义一个中缀表达式,类似扩展函数那样
infix fun LocalDate.formatBy(pattern:String):String{
val formatter = DateTimeFormatter.ofPattern(pattern)
return this.format(formatter)
}
fun main() {
val currentDate = LocalDate.now()
println(currentDate formatBy "yyyy-MM-dd")
// Print: 2024-02-08
(1 until 100).forEach {
println(it)
// Print 1 至 99
}
}
// Class
class Student {
var name = "Lucia"
var semester = "Fall"
var gpa = 3.95
}
fun main() {
var student = Student()
// Instance
println(student.name)
// Prints: Lucia
println(student.semester)
// Prints: Fall
println(student.gpa)
// Prints: 3.95
}
class Student(
val name: String,
val gpa: Double,
val semester: String,
val estimatedGraduationYear: Int
)
fun main() {
val student = Student("Lucia", 3.95, "Fall", 2022)
println(student.name)
// Prints: Lucia
println(student.gpa)
// Prints: 3.95
println(student.semester)
// Prints: Fall
println(student.estimatedGraduationYear)
// Prints: 2022
}
class Student(
val name: String,
val gpa: Double,
val semester: String,
val estimatedGraduationYear: Int
) {
constructor(name: String, gpa: Double) : this(name, gpa, "Fall", 2024)
}
fun main() {
val student = Student("Lucia", 3.95)
println(student.name)
// Prints: Lucia
println(student.semester)
// Prints: Fall
println(student.estimatedGraduationYear)
// Prints: 2024
}
// 具有包含默认值的属性的类
class Student {
var name = "Lucia"
var semester = "Fall"
var gpa = 3.95
}
// 没有类体的简写语法
class Student
class Student(val name: String, val gpa: Double, val semester: String, val estimatedGraduationYear: Int) {
init {
println("$name has ${estimatedGraduationYear - 2020} years left in college.")
}
// 成员函数
fun calculateLetterGrade(): String {
return when {
gpa >= 3.0 -> "A"
gpa >= 2.7 -> "B"
gpa >= 1.7 -> "C"
gpa >= 1.0 -> "D"
else -> "E"
}
}
}
// 创建实例并调用函数时,将执行 when 表达式并返回字母等级
fun main() {
var student = Student("Lucia", 3.95, "Fall", 2022)
// Prints: Lucia has 2 years left in college.
println("${student.name}'s letter grade is ${student.calculateLetterGrade()}.")
// Prints: Lucia's letter grade is A.
}
class Student(val name: String, val gpa: Double, val semester: String, val estimatedGraduationYear: Int) {
init {
println("$name has ${estimatedGraduationYear - 2020} years left in college.")
}
}
fun main() {
var student = Student("Lucia", 3.95, "Fall", 2022)
// Prints: Lucia has 2 years left in college.
}
// 默认实现 getter/setter 和 toString 这些方法
data class UserInfo(
val name: String,
val age: Int
)
fun main() {
val userInfo = UserInfo("张三", 20)
println(userInfo.name)
// 张三
println(userInfo.toString())
// UserInfo(name=张三, age=20)
}
// 私有化构造方法
class User private constructor(val name: String) {
// 伴生对象,相当于一个静态类
companion object {
fun createUser(name: String): User {
return User(name)
}
}
}
fun main() {
// 就像是调用静态方法
val user = User.createUser("张三")
println(user.name)
//Print: 张三
}
class Outer {
val outStr: String = "Outer"
// inner 可以让内部类访问外部类
inner class Inner {
fun printOutStr(){
println(outStr)
}
}
}
fun main() {
val outer = Outer()
outer.Inner().printOutStr()
// Print: Outer
}
如果不用inner修饰,会导致Inner类无法使用outStr
object HttpUtils {
const val baseUrl = "https://xxxx.com"
fun getRequest(url: String): String {
// 示例代码....
return "Result"
}
}
fun main() {
println(HttpUtils.baseUrl)
// Print: "https://xxxx.com"
HttpUtils.getRequest("xxxxx")
}
object类中定义的函数和属性都可以用类名直接引用
- Kotlin 语言官方文档 (kotlinlang.org)