当前位置:网站首页>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
边栏推荐
- Huber Loss
- Introduction Guide to stereo vision (6): level constraints and polar correction of fusiello method
- Summary of "reversal" problem in challenge Programming Competition
- 12. Dynamic link library, DLL
- Driver's license physical examination hospital (114-2 hang up the corresponding hospital driver physical examination)
- Applet data attribute method
- 【PyTorch Bug】RuntimeError: Boolean value of Tensor with more than one value is ambiguous
- kubeadm系列-02-kubelet的配置和启动
- 【ManageEngine】如何利用好OpManager的报表功能
- Hi Fun Summer, play SQL planner with starrocks!
猜你喜欢
Confusion matrix
Huber Loss
编辑器-vi、vim的使用
Global configuration tabbar
Introduction Guide to stereo vision (3): Zhang calibration method of camera calibration [ultra detailed and worthy of collection]
Svgo v3.9.0+
Introduction Guide to stereo vision (4): DLT direct linear transformation of camera calibration [recommended collection]
RT thread kernel quick start, kernel implementation and application development learning with notes
C [essential skills] use of configurationmanager class (use of file app.config)
Nodemon installation and use
随机推荐
Hosting environment API
混淆矩阵(Confusion Matrix)
TF coordinate transformation of common components of ros-9 ROS
Rebuild my 3D world [open source] [serialization-1]
Rebuild my 3D world [open source] [serialization-3] [comparison between colmap and openmvg]
【愚公系列】2022年7月 Go教学课程 003-IDE的安装和基本使用
Codeworks round 681 (Div. 2) supplement
太不好用了,长文章加图文,今后只写小短文
牛顿迭代法(解非线性方程)
Hi Fun Summer, play SQL planner with starrocks!
Progressive JPEG pictures and related
The location search property gets the login user name
2309. The best English letters with both upper and lower case
Rebuild my 3D world [open source] [serialization-2]
Multiple solutions to one problem, asp Net core application startup initialization n schemes [Part 1]
Editor use of VI and VIM
Programming implementation of ROS learning 5-client node
Use and programming method of ros-8 parameters
阿里云发送短信验证码
Kubedm series-00-overview