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

包罗广泛的范围操作符(Inclusive Range Operator)

Swift

for index in 1…5 {

print(“(index) times 5 is (index * 5)”)

}

// 1 times 5 is 5

// 2 times 5 is 10

// 3 times 5 is 15

// 4 times 5 is 20

// 5 times 5 is 25

Kotlin

for (index in 1…5) {

println(“$index times 5 is ${index * 5}”)

}

// 1 times 5 is 5

// 2 times 5 is 10

// 3 times 5 is 15

// 4 times 5 is 20

// 5 times 5 is 25

BASICS

数组

Swift

var shoppingList = [“catfish”, “water”,

“tulips”, “blue paint”]

shoppingList[1] = “bottle of water”

Kotlin

val shoppingList = arrayOf(“catfish”, “water”,

“tulips”, “blue paint”)

shoppingList[1] = “bottle of water”

映射

Swift

var occupations = [

“Malcolm”: “Captain”,

“Kaylee”: “Mechanic”,

]

occupations[“Jayne”] = “Public Relations”

Kotlin

val occupations = mutableMapOf(

“Malcolm” to “Captain”,

“Kaylee” to “Mechanic”

)

occupations[“Jayne”] = “Public Relations”

空集合

Swift

let emptyArray = String

let emptyDictionary = String: Float

Kotlin

val emptyArray = arrayOf()

val emptyMap = mapOf<String, Float>()

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

最后

中年危机是真实存在的,即便有技术傍身,还是难免对自己的生存能力产生质疑和焦虑,这些年职业发展,一直在寻求消除焦虑的依靠。

  • 技术要深入到什么程度?

  • 做久了技术总要转型管理?

  • 我能做什么,我想做什么?

  • 一技之长,就是深耕你的专业技能,你的专业技术。(重点)

  • 独立做事,当你的一技之长达到一定深度的时候,需要开始思考如何独立做事。(创业)

  • 拥有事业,选择一份使命,带领团队实现它。(创业)

一技之长分五个层次

  • 栈内技术 - 是指你的前端专业领域技术

  • 栈外技术 - 是指栈内技术的上下游,领域外的相关专业知识

  • 工程经验 - 是建设专业技术体系的“解决方案”

  • 带人做事 - 是对团队协作能力的要求

  • 业界发声 - 工作经验总结对外分享,与他人交流

永远不要放弃一技之长,它值得你长期信仰持有

开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

主要内容包括html,css,html5,css3,JavaScript,正则表达式,函数,BOM,DOM,jQuery,AJAX,vue 等等。

  • 21
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值