当前位置:网站首页>Kotlin introductory notes (II) a brief introduction to kotlin functions
Kotlin introductory notes (II) a brief introduction to kotlin functions
2022-07-05 09:13:00 【Meng Meng Mu Xi】
Preface : This tutorial is best done with JAVA Study on the basis of
One 、main Use of functions
(1) Did you learn java My friends should know ,java Of main The method is static , And written in class in .Kotlin It's different , In general, you can write main The function is written in class Outside
class Test{
}
/** I am a main Entry function **/
fun main(args: Array<String>) {
var test=Test();
println("main stay class External execution !")
}
Running results :
(2) Of course, there will be main The function is written in class In the method
( This paragraph is reproduced from :https://blog.csdn.net/qq_19306415/article/details/81902197)
class Test{
companion object {
/** I am a main Entry function **/
@JvmStatic
fun main(args: Array<String>) {
var test=Test();
}
}
}
explain :
companion object { // I'm a static block
/**
The static variables and static methods defined here , similar java Of :static
**/
}
amount to java Medium
public static void main(String[] args){
}
Link to the original text :https://blog.csdn.net/qq_19306415/article/details/81902197
Two 、 Ordinary function
Statement : Use fun Keywords to declare (function Abbreviation )
Format :
fun method(param1 : Int , param2 : Int) : Int {
return 0
}
PS: Declare the return value type of the function , If it is Void Don't write ( Omit )
Above method refer to Method name , Parameter one And Parameter two Refers to the parameter name , Method name And Parameter name You can modify it yourself . If you don't want to receive any parameters , Then you can write only a pair of empty parentheses .( This is related to java identical )
Two 、 Built in functions
Definition : from Kotlin Provides a series of built-in functions
Some simple built-in functions :
Such as :Int max(Int num)
usage :( To guide the package : import kotlin.math.max)
fun largerNumber(num1 : Int , num2 : Int) : Int {
return max(num1,num2)
}
There are also some complex built-in functions , Such as
- let function
- also function
- with function
- run function
- apply function
- ...
Not today , But the above is biased Java A way of writing ,Kotlin Provides a more convenient syntax format :
(1) When there is only one line of code in a function ,Kotlin Allow us not to write the function body , You can use the only line of code "=" Connected at the tail
fun largerNumber(num1 : Int , num2 : Int) : Int = max(num1 , num2)
however , In fact, it can be further simplified ...
(2) because max() Function returns Int value ,largerNumber() The equals sign is used in the tail again , therefore Kotlin It can automatically deduce largerNumber() The return of the function must also be a Int value , So you don't have to show the declaration Int
fun largerNumber(num1 : Int , num2 : Int) = max(num1 , num2)
Kotlin The small white , I took notes while studying teacher Guolin's works . If there are any mistakes, please point out ! Thank you for reading !
Reference resources :
Kotlin How to create in a class main function _ New ape blog -CSDN Blog _kotlin main function
《 First line of code Android ( The third edition )》 --- Guo Lin
边栏推荐
- Ecmascript6 introduction and environment construction
- c#比较两张图像的差异
- [code practice] [stereo matching series] Classic ad census: (6) multi step parallax optimization
- Generate confrontation network
- Summary and Reflection on issues related to seq2seq, attention and transformer in hands-on deep learning
- Hosting environment API
- 一题多解,ASP.NET Core应用启动初始化的N种方案[上篇]
- 【PyTorch Bug】RuntimeError: Boolean value of Tensor with more than one value is ambiguous
- C [essential skills] use of configurationmanager class (use of file app.config)
- Ros-10 roslaunch summary
猜你喜欢
Applet global style configuration window
编辑器-vi、vim的使用
生成对抗网络
Applet (subcontracting)
[code practice] [stereo matching series] Classic ad census: (4) cross domain cost aggregation
My experience from technology to product manager
Ros- learn basic knowledge of 0 ROS - nodes, running ROS nodes, topics, services, etc
nodejs_ 01_ fs. readFile
Blogger article navigation (classified, real-time update, permanent top)
一题多解,ASP.NET Core应用启动初始化的N种方案[上篇]
随机推荐
Solution to the problems of the 17th Zhejiang University City College Program Design Competition (synchronized competition)
Introduction Guide to stereo vision (2): key matrix (essential matrix, basic matrix, homography matrix)
Blue Bridge Cup provincial match simulation question 9 (MST)
Oracle advanced (III) detailed explanation of data dictionary
Codeworks round 639 (Div. 2) cute new problem solution
AdaBoost use
Hi Fun Summer, play SQL planner with starrocks!
Configuration and startup of kubedm series-02-kubelet
2020 "Lenovo Cup" National College programming online Invitational Competition and the third Shanghai University of technology programming competition
Introduction Guide to stereo vision (4): DLT direct linear transformation of camera calibration [recommended collection]
Illustrated network: what is gateway load balancing protocol GLBP?
Golang foundation -- map, array and slice store different types of data
信息与熵,你想知道的都在这里了
asp. Net (c)
Confusing basic concepts member variables local variables global variables
信息與熵,你想知道的都在這裏了
Summary and Reflection on issues related to seq2seq, attention and transformer in hands-on deep learning
AUTOSAR从入门到精通100讲(103)-dbc文件的格式以及创建详解
Information and entropy, all you want to know is here
AUTOSAR from getting started to mastering 100 lectures (103) -dbc file format and creation details