1.range表达式
fun main() {
var age = 12.3
if (age in 0.0..3.3){
println("婴幼儿")
}else if (age in 4.0..12.2){
println("少儿")
}else{
println("未知")
}
/**
* in 后面还可以接 list set 都可以
*/
if (age !in 0.0..3.3){
println("婴幼儿")
}
}
2.when表达式
val school = "小s学"
val level = when (school) {
"学前班" -> "幼儿"
"小学" -> "少年"
"中学" -> "青年"
"高中" -> "学生"
else -> {
println("未知")
}
}
println(level)
3.String模板
4.函数
fun main() {
println(doSomething(1, true))
}
private fun doSomething(age:Int,flag:Boolean):String{
return "result $age $flag"
}
5.Unit函数
7.Nothing类型
fun main() {
println(doSomething(1, true))
TODO("nothing")
println("after nothing")
}
private fun doSomething(age:Int,flag:Boolean):String{
return "result $age $flag"
}
8.反引号的函数命名
public class MyJava {
public static void is (){
System.out.println("is invoked");
}
public void saa (){
System.out.println("is invoked");
}
}
Kotlin 调用Java
fun main() {
MyJava.`is`()
MyJava().saa()
}
fun `**~special function with weird name~**`(){
}
9.匿名函数
fun main() {
val total = "Mississippi".count()
val total1 = "Mississippi".count { letter -> letter == 's' }
println(total)
println(total1)
}
10.函数参数
fun main() {
// val blessingFunction:()->String={
// "我很牛逼"
// }
//
// println(blessingFunction())
val blessingFunction ={ name:String,year:Int ->
val holiday = "New Year ."
"$name, Happy $holiday $year"
}
println(blessingFunction("jack",3))
}
11.什么是lambda
12.定义参数是函数的函数
fun main() {
val getDiscountWords ={goodsName:String,hours:Int ->
val currentYear =2027
"${currentYear}年,双十一${goodsName}促销倒计时:$hours 小时"
}
showOnBoard("卫生纸",getDiscountWords)
}
//具名函数
fun showOnBoard(goodsName:String,getDiscountWords:(String,Int) ->String){
val hours:Int = (1..24).shuffled().last()
println(getDiscountWords(goodsName,hours))
}
13.简略写法
fun main() {
showOnBoard1("卫生纸"){goodsName:String,hours:Int ->
val currentYear =2027
"${currentYear}年,双十一${goodsName}促销倒计时:$hours 小时"
}
}
//具名函数
fun showOnBoard1(goodsName:String,getDiscountWords:(String,Int) ->String){
val hours:Int = (1..24).shuffled().last()
println(getDiscountWords(goodsName,hours))
}
14.函数内联 inline 关键字
fun main() {
showOnBoard1("卫生纸"){goodsName:String,hours:Int ->
val currentYear =2027
"${currentYear}年,双十一${goodsName}促销倒计时:$hours 小时"
}
}
//具名函数
inline fun showOnBoard1(goodsName:String,getDiscountWords:(String,Int) ->String){
val hours:Int = (1..24).shuffled().last()
println(getDiscountWords(goodsName,hours))
}
15.函数引用
fun main() {
showOnBoard2("牙膏",::getDiscountWords)
}
fun getDiscountWords(goodsName: String,hours:Int):String{
val currentYear =2027
return "${currentYear}年,双十一${goodsName}促销倒计时:$hours 小时"
}
//具名函数
fun showOnBoard2(goodsName:String,getDiscountWords:(String,Int) ->String){
val hours:Int = (1..24).shuffled().last()
println(getDiscountWords(goodsName,hours))
}
16.函数类型作为返回类型
fun main() {
val configDiscountWords = configDiscountWords()
println(configDiscountWords("Jack"))
}
fun configDiscountWords():(String)->String{
return {goodsName:String ->
val currentYear =2027
val hours:Int = (1..24).shuffled().last()
"${currentYear}年,双十一${goodsName}促销倒计时:$hours 小时"
}
}
17.闭包
18.Lambda匿名内部类
文章来源:https://www.toymoban.com/news/detail-828554.html
都是一种思想,lamdba表达式可以被当作函数参数和返回值传递嘛 在kotlin里文章来源地址https://www.toymoban.com/news/detail-828554.html
到了这里,关于Kotlin基本语法1到函数的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!