当前位置:网站首页>[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é
- Teach you how to realize pynq-z2 bar code recognition
- 洛谷_P1008 [NOIP1998 普及组] 三连击_枚举
- Why can't the start method be called repeatedly? But the run method can?
- FPGA based analog I ² C protocol system design (with main code)
- 固收+产品有什么特点?
- What is the London Silver unit
- 注解学习总结
- I want to buy fixed income + products, but I don't know what its main investment is. Does anyone know?
- PSS: vous n'êtes qu'à deux niveaux du NMS Free + Lifting point | 2021 Paper
猜你喜欢
洛谷_P1007 独木桥_思维
What are the operating modes of the live app? What mode should we choose?
All you want to know about large screen visualization is here
Massive data! Second level analysis! Flink+doris build a real-time data warehouse scheme
Let's talk about the process of ES Indexing Documents
Design and implementation of food recipe and ingredients website based on vue+node+mysql
QT 如何在背景图中将部分区域设置为透明
直播app运营模式有哪几种,我们该选择什么样的模式?
Computer screen splitting method
Talk about redis transactions
随机推荐
洛谷_P1008 [NOIP1998 普及组] 三连击_枚举
[high concurrency] deeply analyze the callable interface
What is the London Silver code
E ModuleNotFoundError: No module named ‘psycopg2‘(已解决)
CentOS8-postgresql初始化时报错:initdb: error: invalid locale settings; check LANG and LC_* environment
#27ES6的数值扩展
Knightctf 2022 web section
Use GCC to generate an abstract syntax tree "ast" and dump it to Dot file and visualization
Use of abortcontroller
volatile与JMM
sql注入原理
Web chat room system based on SSM
优雅的自定义 ThreadPoolExecutor 线程池
Piblup test report 1- pedigree based animal model
洛谷入门2【分支结构】题单题解
PCL Library - error reporting solution: cmake and Anaconda conflicts during installation
[digital signal processing] discrete time signal (analog signal, discrete time signal, digital signal | sampling leads to time discrete | quantization leads to amplitude discrete)
设计原则和思想:设计原则
About sitemap XML problems
ThreadLocal之强、弱、软、虚引用