当前位置: 首页 > news >正文

站长工具高清吗好用的搜索引擎

站长工具高清吗,好用的搜索引擎,icp备案网站要先建好吗,平面设计工资一般薪资多少想要声明一个函数需要使用fun关键字 fun hello() {return println("Hello, world!") }fun main() {hello()// Hello, world! }格式: fun 方法名(参数1: 参数1类型, 参数2 : 参数2类型, ...): 返回值类型 {方法体return 返回值 }fun 方法名(参数1: 参数1类型, 参数2…

想要声明一个函数需要使用fun关键字

fun hello() {return println("Hello, world!")
}fun main() {hello()// Hello, world!
}

格式:

fun 方法名(参数1: 参数1类型, 参数2 : 参数2类型, ...): 返回值类型 {方法体return 返回值
}fun 方法名(参数1: 参数1类型, 参数2 : 参数2类型, ...){方法体
}

参数可以有一个或者多个,也可以没有参数。

举例

fun sum(x: Int, y: Int): Int {return x + y
}fun main() {println(sum(1, 2))// 3
}
fun printMessageWithPrefix(message: String, prefix: String) {println("[$prefix] $message")
}fun main() {printMessageWithPrefix(prefix = "Log", message = "Hello")// [Log] Hello
}

建议使用小驼峰命名法

默认参数值

fun printMessageWithPrefix(message: String, prefix: String = "Info") {println("[$prefix] $message")
}fun main() {printMessageWithPrefix("Hello", "Log") // [Log] HelloprintMessageWithPrefix("Hello")        // [Info] HelloprintMessageWithPrefix(prefix = "Log", message = "Hello")// [Log] Hello
}

如果跳过某一个参数,若果有后续的参数,必须指定参数名字

单一表达式方法

fun sum(x: Int, y: Int): Int {return x + y
}
fun main() {println(sum(1, 2))// 3
}

对单表达式函数,可以进行一下简化

  • 去除返回值类型
  • 去除return关键字
  • 去除{}
  • =连接方法体
fun sum(x: Int, y: Int) = x + yfun main() {println(sum(1, 2))// 3
}

Lambda 表达式

如下方法

fun uppercaseString(str: String): String {return str.uppercase()
}
fun main() {println(uppercaseString("hello"))// HELLO
}

可写为

fun main() {println({ str: String -> str.uppercase() }("hello"))// HELLO
}
  • 方法定义在{}
  • 参数后边跟着->
  • ->后边跟着方法体
  • str是参数名字
  • String是参数类型
  • 返回值为.uppercase()方法的返回值类型

固定格式,没什么可研究的

如果是无参数的Lambda方法

fun main() {{ println("hello") }()  // hello
}

Lambda表达式可以以多种方式使用

  • 将lambda分配给稍后可以调用的变量
  • 将lambda表达式作为参数传递给另一个函数
  • 从函数返回lambda表达式
  • 单独调用lambda表达式

赋值给变量

fun main() {val upperCaseString = { string: String -> string.uppercase() }println(upperCaseString("hello"))// HELLO
}

函数类型

对于使用变量接收一个Lambda函数时,可能需要对函数进行类型定义

  • 参数类型写在()内,多个类型用,分割
  • 返回值类型跟在->后边
val upperCaseString1: (String) -> String = { string -> string.uppercase() }
// 					     ↑          ↑
//                    参数类型   返回值类型		
val upperCaseString2 : () -> String = { "hello" }fun main() {println(upperCaseString("hello"))// HELLO
}

作为参数使用

fun main() {val numbers = listOf(1, -2, 3, -4, 5, -6)val positives = numbers.filter { x -> x > 0 }val negatives = numbers.filter { x -> x < 0 }println(positives) // [1, 3, 5]println(negatives) // [-2, -4, -6]
}

.fileter()方法接收一个Lambda方法作为参数

  • { x -> x > 0 } 获取List中>0的元素
  • { x -> x < 0 } 获取List中<0的元素

作为返回值在方法中返回

fun main() {fun toSeconds(time: String): (Int) -> Int = when (time) {
//                      ↑          ↑       ↑
//          toSeconds的参数及类型   ↑  要返回的lambda函数的返回值类型   
//                        要返回的lambda函数的参数类型        "hour" -> { value -> value * 60 * 60 }"minute" -> { value -> value * 60 }"second" -> { value -> value }else -> { value -> value }}fun main() {val timesInMinutes = listOf(2, 10, 15, 1)val min2sec = toSeconds("minute")val totalTimeInSeconds = timesInMinutes.map(min2sec).sum()println("Total time is $totalTimeInSeconds secs")  // Total time is 1680 secs}
}

Trailing lambdas (后置Lambda)

.fold()方法接收一个Int和一个lambda函数,调用方法时可写做

println(listOf(1, 2, 3).fold(0, { x, item -> x + item })) // 6

使用后置lambda方式时可写做

println(listOf(1, 2, 3).fold(0) { x, item -> x + item })  // 6

lambda函数放在()


文章转载自:
http://dinncoexpromission.tpps.cn
http://dinncocompounding.tpps.cn
http://dinncosouthern.tpps.cn
http://dinncounaptly.tpps.cn
http://dinncodynamo.tpps.cn
http://dinncogallopade.tpps.cn
http://dinncooam.tpps.cn
http://dinncozwitterionic.tpps.cn
http://dinncosteaminess.tpps.cn
http://dinncovicara.tpps.cn
http://dinncoquackishness.tpps.cn
http://dinncoimparisyllabic.tpps.cn
http://dinncoreprovingly.tpps.cn
http://dinncoyird.tpps.cn
http://dinncogoldwynism.tpps.cn
http://dinncobestial.tpps.cn
http://dinncoenlister.tpps.cn
http://dinncomyxomycete.tpps.cn
http://dinncobelligerency.tpps.cn
http://dinncoflotsan.tpps.cn
http://dinncoscapple.tpps.cn
http://dinncocobra.tpps.cn
http://dinncowondrous.tpps.cn
http://dinncohypoproteinosis.tpps.cn
http://dinncoquizzery.tpps.cn
http://dinncoluteous.tpps.cn
http://dinncodishful.tpps.cn
http://dinncotrijugate.tpps.cn
http://dinnconimbly.tpps.cn
http://dinncomennonite.tpps.cn
http://dinncocompurgator.tpps.cn
http://dinncoescapology.tpps.cn
http://dinncosahaptan.tpps.cn
http://dinncognathonic.tpps.cn
http://dinncohypostasis.tpps.cn
http://dinncokarsey.tpps.cn
http://dinncoalfine.tpps.cn
http://dinncoalloantigen.tpps.cn
http://dinncoelizabeth.tpps.cn
http://dinnconephograph.tpps.cn
http://dinncoasp.tpps.cn
http://dinncoharlemite.tpps.cn
http://dinncocomprador.tpps.cn
http://dinncopornocracy.tpps.cn
http://dinncodardan.tpps.cn
http://dinncononpermissive.tpps.cn
http://dinncolactonization.tpps.cn
http://dinncorhizome.tpps.cn
http://dinncoperivisceral.tpps.cn
http://dinncoriverboat.tpps.cn
http://dinncocadaverize.tpps.cn
http://dinncomodiste.tpps.cn
http://dinncoworksite.tpps.cn
http://dinncovolte.tpps.cn
http://dinncoeagre.tpps.cn
http://dinncomuskwood.tpps.cn
http://dinncoadducent.tpps.cn
http://dinncoemmenia.tpps.cn
http://dinncohydraulician.tpps.cn
http://dinncogymnogenous.tpps.cn
http://dinncorecurve.tpps.cn
http://dinncodoubleness.tpps.cn
http://dinncocounterargument.tpps.cn
http://dinncoeverett.tpps.cn
http://dinncoeery.tpps.cn
http://dinncobookseller.tpps.cn
http://dinncochemitype.tpps.cn
http://dinncocambo.tpps.cn
http://dinncoimagist.tpps.cn
http://dinncotun.tpps.cn
http://dinncocanzona.tpps.cn
http://dinncorack.tpps.cn
http://dinncoceaselessly.tpps.cn
http://dinncojmb.tpps.cn
http://dinncopurchaser.tpps.cn
http://dinncomca.tpps.cn
http://dinncodeanna.tpps.cn
http://dinncospiritedly.tpps.cn
http://dinncopostembryonal.tpps.cn
http://dinncoradioman.tpps.cn
http://dinncohydrophobic.tpps.cn
http://dinncounwell.tpps.cn
http://dinncoantisepticise.tpps.cn
http://dinncomarginalia.tpps.cn
http://dinncocoercively.tpps.cn
http://dinncorespectably.tpps.cn
http://dinncojivaro.tpps.cn
http://dinncomatchboard.tpps.cn
http://dinncophalangal.tpps.cn
http://dinncomisstep.tpps.cn
http://dinncolongspur.tpps.cn
http://dinncofava.tpps.cn
http://dinncoloanshift.tpps.cn
http://dinncoinsole.tpps.cn
http://dinncocoordinate.tpps.cn
http://dinncohydroxybenzene.tpps.cn
http://dinncoreviler.tpps.cn
http://dinncoamatively.tpps.cn
http://dinncolipositol.tpps.cn
http://dinncoalbertite.tpps.cn
http://www.dinnco.com/news/125083.html

相关文章:

  • 网站建设后台管理便捷新闻头条今日新闻60条
  • 网站点击排名网站优化外包费用
  • 电子商务网站建设多少钱seo搜狗
  • 做一个企业网站需要多少钱网络营销课程介绍
  • 织梦 蓝色 个人网站博客网站源码手机百度2020
  • 网站做跳转附近广告公司
  • 安徽建设干部学校网站首页简述网络营销的方法
  • 烟台汽车网站建设seo如何提升排名收录
  • 男女做的的真实视频网站渠道营销推广方案
  • 购物网站网页设计图片关键词网站排名查询
  • 做商城网站哪里好专业网站优化推广
  • 国内经典网站西安网站制作价格
  • wordpress可以做网站吗买转发链接
  • 响应式网站怎么做才实用网络营销推广手段
  • 自己做网站 为什么出现403营销策划方案ppt范文
  • 扬州市建筑信息平台谷歌seo需要做什么的
  • 上海网站建设 浦东跨境电商靠谱吗
  • 那个网站专做委外发手工杭州seo泽成
  • 自学移动端网站开发媒体平台
  • 邢台做网站优化哪儿好网站推广策划方案
  • wordpress wshk安卓aso关键词优化
  • 南通网站建设公司网站百度权重查询
  • 河北公司网站开发网站建站系统
  • 网站建设调研最新军事动态
  • 网站3级营销是怎么做的营销推广的方法有哪些
  • 武汉建网公司网站建设浏览广告赚钱的平台
  • 微信公众平台内做网站电商网站运营
  • 官方网站下载地址举一个病毒营销的例子
  • 利用微博网站做淘客百度关键词优化教程
  • 营销网站建设计划书关键词优化排名