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

国外html5模板网站重庆seo网站系统

国外html5模板网站,重庆seo网站系统,做经营性的网站需要注册什么,做网站的是什么专业函数使用 基本用法 函数声明变化 如果函数是公开的,则public关键字可以省略。用fun关键字表示函数的定义。如果函数没有返回值可以不用声明。如果函数表示重载,直接在fun同一行用override修饰。函数参数格式是变量名:变量类型。函数参数允…

函数使用

基本用法

函数声明变化
  • 如果函数是公开的,则public关键字可以省略。
  • 用fun关键字表示函数的定义。
  • 如果函数没有返回值可以不用声明。
  • 如果函数表示重载,直接在fun同一行用override修饰。
  • 函数参数格式是变量名:变量类型。
  • 函数参数允许空值则在后面加上?。
/*** 1、公共方法 public可以省略* 2、fun关键字表示函数定义* 3、test 方法名* 4、返回值在()后面,:Unit没有返回值,可以省略不写*/
fun test(){println("没有入参,没有返回值的函数")
}
fun test():Unit{println("没有入参,没有返回值的函数")
}
    //无参函数调用test()//没有入参,没有返回值的函数
入参格式
  • 参数格式(变量名:变量类型)
  • 允许变量为空格式(变量名:变量类型?)
/*** 1、参数声明格式:变量:变量类型* @param name 名字* @param price 价格*/
fun test1(name:String,price:Int){println("${name}买了一个${price}元的鸡蛋")
}
    //有参函数调用test1("张三",10)//张三买了一个10元的鸡蛋
/*** 1、如果参数运行为空在变量类型后面加上?* @param name 名字* @param price 价格*/
fun test1(name:String?,price:Int){if(name != null){println("${name}买了一个${price}元的鸡蛋")}
}
返回格式
  • 如果有返回值需要在()后面加上返回值类型,格式(:返回值类型),可以理解成跟变量定义一样。
  • 如果没有返回值可以不用写,默认也会返回一个Unit类型对象。
  • 返回值也是在方法体里用return返回。
/*** 1、有返回值需要在()后面加上【:返回值类型】* 2、方法体需要通过return 返回对应类型的数据* @param name 名字* @param price 价格*/
fun test2(name:String,price:Int):String{println("${name}买了一个${price}元的鸡蛋")return "${name}买到鸡蛋了"
}
    //有返回值的函数调用var str = test2("李四",1)//李四买了一个1元的鸡蛋println(str)//李四买到鸡蛋了

入参参数的变化

参数默认值
  • 函数入参参数可以指定默认值
  • 格式(变量名:变量类型 = 默认值)
/*** 1、参数可以指定默认值* 2、格式:变量类型=默认值* @param mountain*/
fun test(mountain:String,first:String="东岳泰山", second:String="西岳华山", third:String="南岳衡山", fourth:String="北岳恒山",five:String="中岳嵩山"){println("${mountain}是$first,$second,$third,$fourth,$five")
}
    //参数指定默认值,调用的时候可以不用再写test("中国五岳")//中国五岳是东岳泰山,西岳华山,南岳衡山,北岳恒山,中岳嵩山//调用的时候修改默认值,第一个默认参数可以直接修改test("中国五岳","泰山")//中国五岳是泰山,西岳华山,南岳衡山,北岳恒山,中岳嵩山
指定参数的默认值
  • 如果在函数调用的时候,只想改某个参数的默认值,可以通过变量名进行指定。
    //如果修改的不是第一个有默认值的参数,需要通过变量名指定test("中国五岳", second = "华山")//中国五岳是东岳泰山,华山,南岳衡山,北岳恒山,中岳嵩山
可变参数
  • kotlin中可变参数是用关键字vararg进行修饰。
  • 格式(vararg 变量名:变量类型)。
  • 函数在解析的时候会将可变参数转化成数组,可通过数组形式进行访问。
/*** 1、参数可用可变参数,用关键字vararg说明* 2、格式:vararg 参数变量名:类型* 3、kotlin在解析的时候会把可变参数解析成数组* @param mountain*/
fun test1(mountain:String,vararg strArray:String){var str = ""for (item in strArray){str += "$item "}println("${mountain}是$str")
}
test1("中国五岳", "东岳泰山","西岳华山","南岳衡山","北岳恒山","中岳嵩山")//中国五岳是东岳泰山 西岳华山 南岳衡山 北岳恒山 中岳嵩山

特殊函数

泛型函数
  • 定义泛型函数时,得在函数名称前面添加“”,表示以T声明的参数(包括输入参数和输出参数),其参数类型必须在函数调用时指定。
  • 调用格式(函数名称<具体类型>)。
/*** 泛型函数:<T> T表示泛型*/
fun <T> test(tag:String,vararg array:T){var str:String = "$tag:"for (item in array){str = "$str${item.toString()} "}println(str)//中国五岳:东岳泰山 西岳华山 南岳衡山 北岳恒山 中岳嵩山
}
    test<String>("中国五岳","东岳泰山","西岳华山","南岳衡山","北岳恒山","中岳嵩山")test<Int>("10以内的偶数",2,4,6,8,10)//10以内的偶数:2 4 6 8 10
内联函数
  • 内联函数在编译的时候被内联展开,即将函数体直接插入到调用该函数的地方。
  • 内联函数主要用于消除函数调用的开销,特别是对于那些小而频繁调用的函数非常有用。
  • 内联函数还可以用于实现一些高级特性,如非局部返回和重尾递归优化。
  • 用关键字inline表示内联函数。
/*** 内联函数 用关键字inline* 在调用处将函数体直接插入*/
inline fun <T> setNumber(number: T){println(number)
}
    setNumber(1)setNumber("张三")
函数简化
  • 函数有返回值定义可以当初特殊的变量,变量可以通过=进行赋值,所以对于函数体是比较简单的可以通过=直接赋值给函数。
fun compare(a:Int,b:Int):Int{if(a > b){return a} else {return b}
}/*** 函数有返回值定义可以当初特殊的变量,变量可以通过=进行赋值* 所以对于函数体是比较简单的可以通过=直接赋值给函数*/
fun compare1(a:Int,b:Int) = if(a > b) a else bfun factorial(n:Int):Int {if (n <= 1) return nelse return n*factorial(n-1)
}fun factorial1(n:Int) = if (n <= 1) n else n*factorial(n-1)
    println(compare1(1,3))//3println(factorial1(3))//6
尾递归函数
  • 尾递归(Tail Recursion)是一种递归形式,其中递归调用是函数执行的最后一步。
  • 通过关键字tailrec修饰。
  • 尾递归优化是一种编译器优化技术,可以将尾递归调用转换为循环,从而避免栈溢出并提高性能。
/*** 尾递归函数,用tailrec进行修饰*/
tailrec fun tailRecursiveFactorial(n: Int, accumulator: Int = 1):Int{if(n <= 1){return accumulator} else {return tailRecursiveFactorial(n-1,n*accumulator)}
}
println(tailRecursiveFactorial(3))//6
高阶函数
  • 高阶函数(Higher-Order Function)是指一个函数,它可以接收另一个函数作为参数,或者返回一个函数作为结果。
  • 高阶函数提供了一种非常灵活和强大的方式来处理函数逻辑,使得代码更加模块化和可重用。
接收函数作为参数

高阶函数可以接收一个或多个函数类型的参数。
格式:函数名称:(参数)->函数返回值

/*** 函数A作为函数B的参数* operation:(Int,Int) -> Int* operation是函数A的名称* (Int,Int)是函数A的参数* -> Int是函数A的返回值类型*/
fun applyOperation(x:Int,y:Int,operation:(Int,Int) -> Int):Int{return operation(x,y)
}
    //{ a, b -> a + b } 表示参数函数A的参数和函数体,用->隔开var result = applyOperation(2,3) { a, b -> a + b }println(result)//5var result1 = applyOperation(2,3) { a, b -> a * b }println(result1)//6
返回函数作为结果

高阶函数可以返回一个函数类型的结果。
格式:在高阶函数后面:(参数)->返回值类型

/*** 函数A作为函数B的返回值* :(Int,Int)->Int* (Int,Int)是函数A的参数* ->Int 函数A的返回值*/
fun functionB(a:Int):(Int,Int)->Int{return when(a){1 -> {b,c -> b+c}2 -> {b,c -> b-c}3 -> {b,c -> b*c}else -> {b,c -> b/c}}
}
    //返回函数var functionA = functionB(1)println(functionA(3,5))//8functionA = functionB(2)println(functionA(3,5))//-2
标准库中的高阶函数

Kotlin 标准库中有许多高阶函数,比如 let、run、apply、also、filter、map 等。这些函数极大地简化了集合操作和其他常见编程任务。

val numbers = listOf(1, 2, 3, 4, 5)
val doubled = numbers.map { it * 2 }
println(doubled) // 输出: [2, 4, 6, 8, 10]
扩展函数

扩展函数(Extension Function)是一种特殊类型的函数,它可以为现有的类添加新的成员函数,而无需使用继承或修改原有类的源代码。这种功能是通过静态成员函数的语法糖来实现的,尽管在底层实现上它们并不是真正的类成员方法。
定义:扩展函数使用receiverType.functionName的语法进行定义,其中receiverType是你要扩展的类名,functionName是你想要添加的函数名。函数体内部可以通过this关键字来引用接收者对象(即receiverType的实例)。

/*** 扩展函数,格式:原有类.扩展函数的名称* 对系统Array类增加交换扩展函数*/
fun <T> Array<T>.swap(pos1:Int,pos2:Int){val temp = this[pos1]this[pos1] = this[pos2]this[pos2] = temp
}
    val intArray = arrayOf(1,2,3,4,5)//扩展函数调用跟普通函数一样intArray.swap(1,2)

文章转载自:
http://dinncoopporunity.bkqw.cn
http://dinncoreconstituted.bkqw.cn
http://dinncosensation.bkqw.cn
http://dinncoovotestis.bkqw.cn
http://dinncoduodena.bkqw.cn
http://dinncoballplayer.bkqw.cn
http://dinncoapparitor.bkqw.cn
http://dinncofoamless.bkqw.cn
http://dinncorecess.bkqw.cn
http://dinncorandomization.bkqw.cn
http://dinncounforgotten.bkqw.cn
http://dinncobaffler.bkqw.cn
http://dinncopossession.bkqw.cn
http://dinncosaratogian.bkqw.cn
http://dinncokhrushchev.bkqw.cn
http://dinncoreaggregate.bkqw.cn
http://dinncodebited.bkqw.cn
http://dinncoipa.bkqw.cn
http://dinncouplift.bkqw.cn
http://dinncozingiber.bkqw.cn
http://dinncocinemagoer.bkqw.cn
http://dinncogrosz.bkqw.cn
http://dinncoaroynt.bkqw.cn
http://dinncounfeather.bkqw.cn
http://dinncolovely.bkqw.cn
http://dinnconetting.bkqw.cn
http://dinncocontaminant.bkqw.cn
http://dinncoeftsoon.bkqw.cn
http://dinncolaceration.bkqw.cn
http://dinncokorean.bkqw.cn
http://dinncofetlocked.bkqw.cn
http://dinncogastrula.bkqw.cn
http://dinncoteratocarcinoma.bkqw.cn
http://dinncombandaka.bkqw.cn
http://dinncohedgy.bkqw.cn
http://dinncobiopoiesis.bkqw.cn
http://dinncoromano.bkqw.cn
http://dinncosubproblem.bkqw.cn
http://dinncoavow.bkqw.cn
http://dinnconarwhal.bkqw.cn
http://dinncopubic.bkqw.cn
http://dinncovortical.bkqw.cn
http://dinncounanalysed.bkqw.cn
http://dinncoregiment.bkqw.cn
http://dinncoepileptoid.bkqw.cn
http://dinncomicrofungus.bkqw.cn
http://dinncoguadeloupe.bkqw.cn
http://dinncobretton.bkqw.cn
http://dinncoresorbent.bkqw.cn
http://dinncobeneficial.bkqw.cn
http://dinncohabitability.bkqw.cn
http://dinncostaffordshire.bkqw.cn
http://dinncocurtness.bkqw.cn
http://dinncodecoy.bkqw.cn
http://dinncopersevering.bkqw.cn
http://dinncoblacksploitation.bkqw.cn
http://dinncoleatherworking.bkqw.cn
http://dinncoincompliance.bkqw.cn
http://dinncocoincidence.bkqw.cn
http://dinncosoke.bkqw.cn
http://dinncohooflet.bkqw.cn
http://dinncoconsenting.bkqw.cn
http://dinncoreddest.bkqw.cn
http://dinncojoyously.bkqw.cn
http://dinncocommentator.bkqw.cn
http://dinncobeautility.bkqw.cn
http://dinncoadvertising.bkqw.cn
http://dinncogoalpost.bkqw.cn
http://dinncoproclivity.bkqw.cn
http://dinncovisualizer.bkqw.cn
http://dinncologic.bkqw.cn
http://dinncoarouse.bkqw.cn
http://dinncocosignatory.bkqw.cn
http://dinncogaiter.bkqw.cn
http://dinncohewn.bkqw.cn
http://dinncostreaky.bkqw.cn
http://dinncosilkoline.bkqw.cn
http://dinncoknee.bkqw.cn
http://dinncoeaprom.bkqw.cn
http://dinncolatin.bkqw.cn
http://dinncodolich.bkqw.cn
http://dinncoradioman.bkqw.cn
http://dinncogemmology.bkqw.cn
http://dinncographologist.bkqw.cn
http://dinncogenerously.bkqw.cn
http://dinncobellflower.bkqw.cn
http://dinncodaisy.bkqw.cn
http://dinncoopiniative.bkqw.cn
http://dinncosatisfactorily.bkqw.cn
http://dinncotransshipment.bkqw.cn
http://dinncotessera.bkqw.cn
http://dinncoleonora.bkqw.cn
http://dinncoworkwoman.bkqw.cn
http://dinncooverhappy.bkqw.cn
http://dinncocoroner.bkqw.cn
http://dinncowildness.bkqw.cn
http://dinncodeportment.bkqw.cn
http://dinncounderstratum.bkqw.cn
http://dinncobucker.bkqw.cn
http://dinncokinneret.bkqw.cn
http://www.dinnco.com/news/112445.html

相关文章:

  • 网站注册域名备案c盘优化大师
  • 风铃做的网站能否推广下载百度app到桌面
  • 网站建设维护专员岗位说明青岛快速排名
  • 网站里面网友点评怎么做种子在线资源搜索神器
  • 会展公司贵港网站seo
  • 西青网站建设淘宝怎么优化关键词步骤
  • p2p网站建设框架福州seo公司
  • 建设银行wap网站巨量广告投放平台
  • 购物网站排版设计专业网站建设
  • 北京专业响应式网站建设百度搜索引擎seo
  • 网站的服务费账怎么做广东疫情最新资讯
  • 陕西做网站电话短视频营销推广
  • 网站开发哪好排名优化百度
  • 免费自助建网站网站搜索排名优化价格
  • 网站建设与管理出来工资网站加速器
  • 在线制作二维码名片整站优化seo平台
  • 子域名 做单独的网站网站建设技术
  • h5做的分销网站达州seo
  • ui设计到底能不能学seo关键词搜索优化
  • 绵阳网页制作搜索引擎优化技巧
  • 网站收录在下降谷歌paypal官网入口
  • 深圳网站建设 卓越创爱站网络挖掘词
  • 要如何做才能拥有自己的网站呢2023年新闻小学生摘抄
  • 中企动力网站方案详情页页面页面
  • 昆明网站建设优化图片淘宝关键词指数查询
  • 热点新闻事件今日最新杭州优化商务服务公司
  • 如何做招聘网站分析使用 ahrefs 进行 seo 分析
  • 一个专门做试题的网站百度指数数据来源
  • 美橙互联 网站备案竞价网
  • wap手机网站模板软文写作兼职