当前位置:网站首页>[kotlin] the next day
[kotlin] the next day
2022-06-27 15:29:00 【No. 101 of the top 100 most handsome faces in the Asia Pacific 】
️ Author: intellectuals
️ Personal blog : Old nine CSDN Blog
Personal famous saying : Uncontrollable things Optimistic face
Series column :kotlin Rapid clearance
List of articles
- Characteristics of function names in backquotes
- Anonymous functions
- Function types and implicit returns
- Learning function parameters
- it keyword
- Type inference of anonymous functions
- lambda expression
- A function in which a parameter is defined as a function
- Function inlining (inline)
- Function reference (::)
- Function type as return type
- Anonymous and named functions
Characteristics of function names in backquotes
fun main() {
` Login function 20220622 In the test environment `("Derry","123456")
}
private fun ` Login function 20220622 In the test environment `(name:String,pwd:String){
println(" User name is $name, The password is $pwd")
}
Anonymous functions
fun main() {
val len = "Derry".count()
println(len)
val len2 = "Derry".count{
//it Equivalent to D e r r y The characters of Char
it == 'r'
}
println(len2)
}
Function types and implicit returns
fun main() {
// First step : Declaration of function input and output
val methodAction : () -> String
// The second step : The implementation of the above functions
methodAction = {
val inputValue = 9999999
"$inputValue Derry"
// Do not write anonymous functions return, The last line is the return value
}
// The third step , Call this function
println(methodAction())
}
Learning function parameters
fun main() {
// First step : Declaration of function input and output , Implementation of declarative functions
val methodAction :(Int,Int,Int) -> String ={
number1,number2,number3 ->
val inputValue = 99999
"$inputValue Derry : $number1,$number2,$number3"
}
// The second step , Call this function
println(methodAction(1, 2, 3))
}
it keyword
fun main() {
// One parameter defaults to it
val methodAction2 :(String) -> String = {
"$it"
}
println(methodAction2("DDD"))
val methodAction3 :(Double) ->String = {
"$it"
}
println(methodAction3(23.3))
}
Type inference of anonymous functions
fun main() {
// Anonymous functions , The type is inferred as String
val method1 = {
v1:Double,v2:Float,v3:Int ->
"v1:$v1,v2:$v2,v3:$v3"
}
println(method1(34.3, 34.3f, 99))
val methdo2 = {
335.2f
}
println(methdo2())
val method3 = {
number:Int->
number
}
println(method3(4))
}
lambda expression
fun main() {
// Anonymous functions == lambda expression
val addResultMethod = {
number1 : Int,number2 : Int->
" The result of adding two numbers is :${
number1+number2}"
}
println(addResultMethod(1, 1))
val weekResultMethod = {
number:Int ->
when(number){
1 -> " week 1"
2 -> " week 2"
3 -> " week 3"
4 -> " week 4"
5 -> " week 5"
else -> -1
}
}
println(weekResultMethod(2))
}
A function in which a parameter is defined as a function
fun main() {
loginAPI("lmp","123456"){
msg:String,code:Int ->
println(" The final login is as follows :msg:$msg,code:$code")
}
}
const val USER_NAME_SAVE_DB = "lmp"
const val USER_PWD_SAVE_DB = "123456"
// Sign in API
fun loginAPI(username:String,userpwd:String,responseResult: (String,Int)->Unit){
if(username == null || userpwd == null){
TODO(" The user name and password are null")
}
// Do a lot of checks
if(username.length >= 3 && userpwd.length >= 3){
if(webServiceLoginAPI(username,userpwd)){
// Login successful
responseResult("login success",200)
}else{
// Login failed
responseResult("login error",404)
}
}else{
TODO(" The user name and password are unqualified ")
}
}
// The server
private fun webServiceLoginAPI(name:String,pwd:String) : Boolean{
return if(name == USER_NAME_SAVE_DB && pwd == USER_PWD_SAVE_DB) true else false
}
Function inlining (inline)
package com.lmp.s1
import com.lmp.s1.webServiceLoginAPI as webServiceLoginAPI
/** * Description: * User: lmp * Date: 2022-06-17 * Time: 11:47 */
// simulation : database
const val USER_NAME_SAVE_DB = "lmp"
const val USER_PWD_SAVE_DB = "123456"
fun main() {
loginAPI("lmp","123456"){
msg:String,code:Int ->
println(" The final login is as follows :msg:$msg,code:$code")
}
}
// This function has lambda As a parameter , Inline required
// Inline keyword inline, First be with c++ Of define
public inline fun loginAPI(username:String,userpwd:String,responseResult : (String,Int) -> Unit){
if(username == null || userpwd == null){
TODO(" The user name or password is null")
}
if(username.length >= 3 && userpwd.length >=3){
if(webServiceLoginAPI(username,userpwd)){
// Login successful
responseResult("login success",200)
}else{
// Login failed
responseResult("login error",404)
}
}else{
TODO(" The user name or password is not qualified ")
}
}
// This function does not use lambda As a parameter , There is no need to declare parameters as inline
fun webServiceLoginAPI(name:String,pwd:String) : Boolean{
return name == USER_NAME_SAVE_DB && pwd == USER_PWD_SAVE_DB
}
Function reference (::)
package com.lmp.s1
fun main() {
//lambda The function type of the object belongs to , Need to put methodxx Ordinary functions become objects of function type
//login("lmp", "1234567",::methodResponseResult)
val obj = ::methodResponseResult
val obj2 = obj
val obj3 = obj2
login("lmp","123456",obj3)
}
fun methodResponseResult(msg:String,code:Int){
println(" The result of the final landing is .msg:$msg,code:$code")
}
const val USER_NAME_SAVE_DB = "lmp"
const val USER_PWD_SAVE_DB = "123456"
inline fun login(name:String,pwd:String,responseResult:(String,Int) -> Unit){
if(USER_NAME_SAVE_DB == name && USER_PWD_SAVE_DB == pwd){
// Login successful
responseResult(" Landing successful ",200)
}else{
responseResult(" Login failure error ",404)
}
}
Function type as return type
package com.lmp.s1
fun main() {
val showMethod = show("show")
println(showMethod("lmp",100))
}
fun show(info : String) :(String,Int) -> String{
println(" I am a show function info:$info")
//return An anonymous function
return {
name : String , age : Int ->
" I'm an anonymous function , my name:$name,age:$age"
}
}

Anonymous and named functions
package com.lmp.s1
fun main() {
// Anonymous functions
showPersonInfo("lisi",99,' male '," Study KT Language "){
println(" Show results :$it")
}
// Named functions
showPersonInfo("wangwu",89,' Woman '," Study C++ Language ",::showResultImpl)
}
fun showResultImpl(result:String){
println(" Show results :$result")
}
inline fun showPersonInfo(name:String,age:Int,sex:Char,study:String,showResult:(String)->Unit)
{
val str = "name:$name,age:$age,sex:$sex,study:$study"
showResult(str)
}
————————————————————————
It's not easy to code words , Everyone's support is my driving force to stick to it
Copyright notice : This paper is about CSDN Blogger 「 The top 100 most handsome faces in the Asia Pacific region 101 name 」 The original article of
边栏推荐
- 洛谷_P1007 独木桥_思维
- CAS之比较并交换
- ReentrantLock、ReentrantReadWriteLock、StampedLock
- Using redis skillfully to realize the like function, isn't it more fragrant than MySQL?
- Massive data! Second level analysis! Flink+doris build a real-time data warehouse scheme
- 手机号码的格式
- What is the London Silver code
- ThreadLocal之强、弱、軟、虛引用
- 我想買固收+產品,但是不了解它主要投資哪些方面,有人知道嗎?
- 老师能给我说一下固收+产品主要投资于哪些方面?
猜你喜欢

Pychart installation and setup

Teach you how to realize pynq-z2 bar code recognition

What are the operating modes of the live app? What mode should we choose?

Pycharm安装与设置

What kind of experience is it to read other people's code

Professor huangxutao, a great master in CV field, was born at the age of 86. UIUC specially set up a doctoral scholarship to encourage cutting-edge students

VS编译遇到的问题

Knowledge map model

Buuctf Misc

Is flutter easy to learn? How to learn? The most complete introduction and actual combat of flutter in history. Take it away without thanks~
随机推荐
手机号码的格式
Unity3d best practices: folder structure and source control
Creation and use of static library (win10+vs2022
About the meaning of the first two $symbols of SAP ui5 parameter $$updategroupid
我想买固收+产品,但是不了解它主要投资哪些方面,有人知道吗?
关于 Spartacus 的 sitemap.xml 问题
[MySQL] query valid data based on time
E ModuleNotFoundError: No module named ‘psycopg2‘(已解决)
HTTP Caching Protocol practice
522. 最长特殊序列 II / 剑指 Offer II 101. 分割等和子集
R language triple becomes matrix matrix becomes triple
[xman2018 qualifying] pass
Different perspectives
All you want to know about large screen visualization is here
Eolink 推出面向中小企业及初创企业支持计划,为企业赋能!
数学建模经验分享:国赛美赛对比/选题参考/常用技巧
Julia1.1 installation instructions
ERROR L104: MULTIPLE PUBLIC DEFINITIONS
Pycharm安装与设置
Go error collection | when a function uses a return value with a parameter name