当前位置:网站首页>[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
边栏推荐
- élégant pool de threadpoolexecutor personnalisé
- 522. 最长特殊序列 II / 剑指 Offer II 101. 分割等和子集
- SQL parsing practice of Pisa proxy
- Can the teacher tell me what the fixed income + products are mainly invested in?
- QT notes (XXVIII) using qwebengineview to display web pages
- 522. longest special sequence II / Sword finger offer II 101 Split equal sum subset
- 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
- Top ten Devops best practices worthy of attention in 2022
- 2022-06-27日报:Swin Transformer、ViT作者等共话:好的基础模型是CV研究者的朴素追求
- PSS:你距离NMS-free+提点只有两个卷积层 | 2021论文
猜你喜欢
随机推荐
Design and implementation of reading app based on Web Platform
[issue 17] golang's one-year experience in developing Meitu
2022-06-27日报:Swin Transformer、ViT作者等共话:好的基础模型是CV研究者的朴素追求
[high concurrency] deeply analyze the callable interface
February 16, 2022 freetsdb compilation and operation
volatile与JMM
Synchronized and lock escalation
Lei Jun lost another great general, and liweixing, the founding employee of Xiaomi No. 12, left his post. He once had porridge to create Xiaomi; Intel's $5.4 billion acquisition of tower semiconductor
ERROR L104: MULTIPLE PUBLIC DEFINITIONS
Knowledge map model
Why can't the start method be called repeatedly? But the run method can?
Go error collection | when a function uses a return value with a parameter name
Pychart installation and setup
CNN convolutional neural network (the easiest to understand version in History)
R language objects are stored in JSON
Interview question: rendering 100000 data solutions
Admixture usage document Cookbook
Atomic operation class
AQS抽象队列同步器
Interpretation of new version features of PostgreSQL 15 (including live Q & A and PPT data summary)








