Kotlin与Swift 从零开始(一)基础语法

FUNCTIONS

函数

Swift

func greet(_ name: String,_ day: String) -> String {

return “Hello (name), today is (day).”

}

greet(“Bob”, “Tuesday”)

Kotlin

fun greet(name: String, day: String): String {

return “Hello $name, today is $day.”

}

greet(“Bob”, “Tuesday”)

元组返回

Swift

func getGasPrices() -> (Double, Double, Double) {

return (3.59, 3.69, 3.79)

}

Kotlin

data class GasPrices(val a: Double, val b: Double,

val c: Double)

fun getGasPrices() = GasPrices(3.59, 3.69, 3.79)

参数的变量数目(Variable Number Of Arguments)

Swift

func sumOf(_ numbers: Int…) -> Int {

var sum = 0

for number in numbers {

sum += number

}

return sum

}

sumOf(42, 597, 12)

Kotlin

fun sumOf(vararg numbers: Int): Int {

var sum = 0

for (number in numbers) {

sum += number

}

return sum

}

sumOf(42, 597, 12)

// sumOf() can also be written in a shorter way:

fun sumOf(vararg numbers: Int) = numbers.sum()

函数类型

Swift

func makeIncrementer() -> (Int -> Int) {

func addOne(number: Int) -> Int {

return 1 + number

}

return addOne

}

let increment = makeIncrementer()

increment(7)

Kotlin

fun makeIncrementer(): (Int) -> Int {

val addOne = fun(number: Int): Int {

return 1 + number

}

return addOne

}

val increment = makeIncrementer()

increment(7)

// makeIncrementer can also be written in a shorter way:

fun makeIncrementer() = fun(number: Int) = 1 + number

映射

Swift

let numbers = [20, 19, 7, 12]

numbers.map { 3 * $0 }

Kotlin

val numbers = listOf(20, 19, 7, 12)

numbers.map { 3 * it }

排序

Swift

var mutableArray = [1, 5, 3, 12, 2]

mutableArray.sort()

Kotlin

listOf(1, 5, 3, 12, 2).sorted()

命名参数

Swift

func area(width: Int, height: Int) -> Int {

return width * height

}

area(width: 2, height: 3)

Kotlin

fun area(width: Int, height: Int) = width * height

area(width = 2, height = 3)

// This is also possible with named arguments

area(2, height = 2)

area(height = 3, width = 2)

CLASSES

声明

Swift

class Shape {

var numberOfSides = 0

func simpleDescription() -> String {

return “A shape with (numberOfSides) sides.”

}

}

Kotlin

class Shape {

var numberOfSides = 0

fun simpleDescription() =

“A shape with $numberOfSides sides.”

}

用法

Swift

var shape = Shape()

shape.numberOfSides = 7

var shapeDescription = shape.simpleDescription()

Kotlin

var shape = Shape()

shape.numberOfSides = 7

var shapeDescription = shape.simpleDescription()

子类

Swift

class NamedShape {

var numberOfSides: Int = 0

let name: String

init(name: String) {

self.name = name

}

func simpleDescription() -> String {

return “A shape with (numberOfSides) sides.”

}

}

class Square: NamedShape {

var sideLength: Double

init(sideLength: Double, name: String) {

self.sideLength = sideLength

super.init(name: name)

self.numberOfSides = 4

}

func area() -> Double {

return sideLength * sideLength

}

override func simpleDescription() -> String {

return "A square with sides of length " +

sideLength + “.”

}

}

let test = Square(sideLength: 5.2, name: “square”)

test.area()

test.simpleDescription()

Kotlin

open class NamedShape(val name: String) {

var numberOfSides = 0

open fun simpleDescription() =

“A shape with $numberOfSides sides.”

}

class Square(var sideLength: BigDecimal, name: String) :

NamedShape(name) {

init {

numberOfSides = 4

}

fun area() = sideLength.pow(2)

override fun simpleDescription() =

“A square with sides of length $sideLength.”

}

val test = Square(BigDecimal(“5.2”), “square”)

test.area()

test.simpleDescription()

类型检查

Swift

var movieCount = 0

var songCount = 0

for item in library {

if item is Movie {

movieCount += 1

} else if item is Song {

songCount += 1

}

}

Kotlin

var movieCount = 0

var songCount = 0

for (item in library) {

if (item is Movie) {

++movieCount

} else if (item is Song) {

++songCount

}

}

模式匹配

Swift

let nb = 42

switch nb {

case 0…7, 8, 9: print(“single digit”)

case 10: print(“double digits”)

case 11…99: print(“double digits”)

case 100…999: print(“triple digits”)

default: print(“four or more digits”)

}

Kotlin

val nb = 42

when (nb) {

in 0…7, 8, 9 -> println(“single digit”)

10 -> println(“double digits”)

in 11…99 -> println(“double digits”)

in 100…999 -> println(“triple digits”)

else -> println(“four or more digits”)

}

类型向下转换

Swift

for current in someObjects {

if let movie = current as? Movie {

print("Movie: ‘(movie.name)’, " +

“dir. (movie.director)”)

}

}

Kotlin

for (current in someObjects) {

if (current is Movie) {

println("Movie: ‘${current.name}’, " +

“dir. ${current.director}”)

}

}

协议

Swift

protocol Nameable {

func name() -> String

}

func f<T: Nameable>(x: T) {

print("Name is " + x.name())

}

最后

资料过多,篇幅有限

自古成功在尝试。不尝试永远都不会成功。勇敢的尝试是成功的一半。

加入社区》https://bbs.youkuaiyun.com/forums/4304bb5a486d4c3ab8389e65ecb71ac0
name)’, " +

“dir. (movie.director)”)

}

}

Kotlin

for (current in someObjects) {

if (current is Movie) {

println("Movie: ‘${current.name}’, " +

“dir. ${current.director}”)

}

}

协议

Swift

protocol Nameable {

func name() -> String

}

func f<T: Nameable>(x: T) {

print("Name is " + x.name())

}

最后

[外链图片转存中…(img-FlRb9tKE-1725679549807)]

[外链图片转存中…(img-ILdfHXA5-1725679549809)]

资料过多,篇幅有限

自古成功在尝试。不尝试永远都不会成功。勇敢的尝试是成功的一半。

加入社区》https://bbs.youkuaiyun.com/forums/4304bb5a486d4c3ab8389e65ecb71ac0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值