Kotlin Introduction (ii)--function Fun_kotlin

Source: Internet
Author: User
Website

Website links. Defined

Functions are defined by the Fun keyword. Parameters

The parameter is defined in the form: Name:type. Parameter Default value

Each parameter can have a default value. Such as:

Fun Main (args:array<string>) {
	println (Test (2))
}
fun Test (A:int,b:int = 3): The default value for int{//b is 3, So when you call, you can just pass a value that is assigned a value. return
	a+b
}
Parameters that have default values can also be assigned values. You can also assign values to a specified parameter by the parameter name = value. Such as:
Fun Main (args:array<string>) {
	reformat ("HHH", wordseparator= "xxx")//is assigned to the specified parameter
fun reformat ( str:string,
    Dividebycamelhumps:boolean = False,
    wordseparator:string = "_"
) {
	println (str+ wordseparator+dividebycamelhumps)//hhhxxxfalse
}

Reformat defines three parameters, and the following two have no default values. When invoked, the specified Wordseparator parameter is assigned by the parameter name, and the dividebycamelhumps is not assigned, so its value is the default value. Variable parameter vararg

Define variable parameters by vararg keyword. Variable parameters can be treated as an array. Note that a function can have only one variable parameter. However, the variable parameter is not necessarily the last parameter. As follows:

Fun Test (vararg a:string,b:string) {
	for (T in a) {
		println (t)
	}
	println (b)
}
	Test ("Fdaf", " Fdaf2 "," Fda3 ", b=" 00FDAF ")//Calling Mode

Parameter A is a variable parameter, but it is not the first parameter.

When called, the parameter B is assigned by means of the parameter name = value, otherwise the assigned value is passed to the variable parameter a.

To turn on the class of the KT file above, you can find that the corresponding Java code for this function is as follows:


As you can see, variable parameter A has become a string array. Generic type

Specify the generic type by <>, and the generics are written before the function name. function infix notation can define a function by infix keyword, it needs to meet the following conditions: 1, must be a member function or extension function 2, must be a single parameter function 3, use the infix keyword. As follows:

Fun Main (args:array<string>) {
	println ("one". Test ("All")
}
infix fun string.test (a:string): String {return
	This+a//Use this to represent the caller's value
}
This functionality is equivalent to extending the functionality for string types, which can be used directly by the test function for each string type. Note: Be sure to specify a class to extend functionality before the function name. View its corresponding class file as follows: You can see that $receiver is of type string, which is the type specified before the function name. Top level function

In Kotlin, you don't need to define a function in a class (such a function is called the top level function), it can be defined directly into any KT file--the outer layer does not need to be wrapped in class. Of course, this is only the difference between writing, in fact, after turning into a class file, the function is still a member function of a class. The following is a A.kt source code:

Fun Main (args:array<string>) {
	test ("Fdaf", "fdaf2", "Fda3", b= "00fdaf")
}

Fun Test (vararg a:string , b:string) {for
	(T in a) {
		println (t)
	}
	println (b)
}

Test and the main function are not defined inside of any class, open the file compiled into the class file, as follows:

It can be found that in the corresponding Java file, a class with class name Akt is generated in the outer layer and all functions are defined in the class. Partial functions (local function)

A function can be defined in the contents of another function. As follows:

Fun Test (vararg a:string,b:string) {
	fun inner (i:string) {//define the contents of another function
		println (i+ ", +b)
	}
	Inner (" _ __inner__ ")/directly call the content function
}

viewing its corresponding class file, you can find that internal functions are converted to member functions of a Lambda object. As follows:


As you can see, local functions also have only the outer functions that are accessible and inaccessible elsewhere. member functions

A function can be defined inside a class. Same as Java. Single statement function

When the body of a function has only one expression, you can omit the curly braces and define the function body after the return type and connect by an equal sign. As follows:

Fun Test (A:int,b:int = 3): Int = a*b
There is only one expression a*b the function body of the function, so you can omit the curly braces and connect them with an equal sign.

Also, the return value type of the function can be omitted. As follows:

Fun Test (A:int,b:int = 3) = a*b//omitted the return value type
A single statement function does not need to write return.
return value

The return value is written after the argument parenthesis, separated by a colon. As follows:

Fun Test (A:int,b:int = 3): int{//returns INT type return
	a+b
}
If a function does not return a value, it can write the return value type as unit, and the return value type can be omitted.





























Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.