kotlin函数类比java中的方法;
函数访问层级:
一、顶层函数;
顶层函数访问域为全局,类似java中的全局方法,不需要具体制定类,直接声明函数并实现即可在全局任意位置调用;
示例:
定义:
package com.example.modulethree.util
/** Int转Double*/
fun double(i:Int) : Double{
var i_double = i.toDouble()
return i_double
}
调用:
package com.example.modulethree.binder
import com.example.modulethree.delegate.KotlinMVPTestActivityDelegate
import com.example.modulethree.util.double
import com.example.mvp.binder.BaseDataBinder
class KotlinTestLayoutBinder : BaseDataBinder<KotlinMVPTestActivityDelegate,String>{
override fun viewBindModel(viewDelegate: KotlinMVPTestActivityDelegate?, data: String?) {
viewDelegate?.setText1(data)
var index = double(10)
}
override fun getDataClass(): Class<String> {
return String::class.java
}
}
package com.example.modulethree.binder
import com.example.modulethree.delegate.KotlinTestFragmentDelegate
import com.example.modulethree.util.double
import com.example.mvp.binder.BaseDataBinder
class KotlinTestFragmentBinder : BaseDataBinder<KotlinTestFragmentDelegate,String>{
override fun viewBindModel(viewDelegate: KotlinTestFragmentDelegate?, data: String?) {
var index2 = double(7)
}
override fun getDataClass(): Class<String> {
return String::class.java
}
}
可以在项目中任意代码位置调用此方法,直接通过函数名访问,但是仅限当前module内;
扩展函数:
扩展某个类型的函数,增加对应的函数方法;
示例:
/** Int转Double*/
fun double(i:Int) : Double{
var i_double = i.toDouble()
return i_double
}
/** 扩展函数,扩展Double类型新增函数,Double类型均可调用此函数*/
fun Double.toString1() : String{
var obj = this
if(obj > 5){
return "100"
}
return obj.toString()
}
调用(所有Double类型的数据对象均可调用此方法,具体使用场景应依据实际情况而定):
var index = double(10)
Log.e("indexStr:",index.toString1())
输出结果:
07-03 10:43:26.475 23239-23239/com.example.pc.routingtestproject E/indexStr:: 100
二、类构造函数(个人理解为java中的类构造函数);
*在Kotlin
中,允许有一个主构造函数和多个二级构造函数(辅助构造函数)。其中主构造函数是类头的一部分。
*关键字或者构造函数名:constructor(参数)
1、主构造函数:
package com.example.modulethree.util.function
import android.util.Log
/**
* Created by sun.li on 2018/7/3.
* @author sun.li
*/
class FunTest constructor(index : Int){
init {
if(index > 50){
Log.e("FunTest:","index大于50")
}else{
Log.e("FunTest:","index小于等于50")
}
}
}
等同于(因为是默认的可见性修饰符且不存在任何的注释符,所以主构造函数constructor关键字可以省略):
class FunTest(index : Int){
init {
if(index > 50){
Log.e("FunTest:","index大于50")
}else{
Log.e("FunTest:","index小于等于50")
}
}
}
说明:
*构造函数中不能出现其他的代码,只能包含初始化代码,包含在初始化代码块中;
*关键字:init{...};
*注意点,init{...}中能使用构造函数中的参数;
什么时候什么场景可以省略constructor?
*在构造函数不具有注释符或默认可见性修饰符时,constructor关键字可以省略;
*默认的可见修饰符为public时,可以省略不写;
使用示例:
FunTest(50)
FunTest(55)
结果:
07-03 11:09:07.909 24040-24040/com.example.pc.routingtestproject E/FunTest:: index小于等于50
07-03 11:09:07.909 24040-24040/com.example.pc.routingtestproject E/FunTest:: index大于50
2、辅助(二级)构造函数:
*kotlin支持二级构造函数,它们已constructor关键字作为前缀;
示例:
package com.example.modulethree.util.function
import android.util.Log
/**
* Created by sun.li on 2018/7/3.
* @author sun.li
*/
class FunTest constructor(index : Int=49){
init {
if(index > 50){
Log.e("FunTest:","index大于50")
}else{
Log.e("FunTest:",index.toString())
}
}
constructor(index: Int,num : Int) : this(index){
Log.e("FunTest:","index=$index \t num=$num")
}
}
使用示例:
FunTest()
FunTest(50)
FunTest(55)
FunTest(100,33)
结果:
07-03 11:45:00.713 25097-25097/com.example.pc.routingtestproject E/FunTest:: 50
07-03 11:45:00.713 25097-25097/com.example.pc.routingtestproject E/FunTest:: index大于50
07-03 11:45:00.713 25097-25097/com.example.pc.routingtestproject E/FunTest:: index大于50
07-0311:45:00.713 25097-25097/com.example.pc.routingtestproject E/FunTest:: index=100 num=33
说明:
*二级构造函数中的参数index是委托了主构造函数的参数index;
*主构造函数参数设置默认值时,编译器会生成一个额外的无参构造函数,无参构造函数调用时会执行之构造函数的逻辑,index为默认值;
*当传入1个参数时只会执行主函数init代码块中的代码;
*当传入2个参数时不但会执行init代码块中的代码,还会执行二级构造函数中的代码;
*函数的入参均可以设置相应的默认值;
三、类成员函数
1、成员函数:
package com.example.modulethree.util.function
import android.util.Log
/**
* Created by sun.li on 2018/7/3.
* @author sun.li
*/
class ClassFunTest{
fun foo() : Int{
return 22
}
fun foo1(){
Log.e("Foo:",foo().toString())
}
}
调用:
var foo = ClassFunTest().foo()
ClassFunTest().foo1()
结果:
07-03 12:10:28.505 26480-26480/com.example.pc.routingtestproject E/Foo:: 22
函数内函数的使用示例:
fun foo2(){
fun foo3(){
Log.e("foo3:",foo().toString())
}
foo3()
}
调用示例:
ClassFunTest().foo2()
结果
07-03 12:12:23.507 26480-26480/com.example.pc.routingtestproject E/Foo3:: 22
函数的基本使用先整理到这里,下一篇整理函数学习进阶;