Kotlin最初接触时碰的坑

// app/build.gradle中添加相关配置引入ButterKnifer
dependencies {
     implementation 'com.jakewharton:butterknife:8.8.1'
     kapt 'com.jakewharton:butterknife-compiler:8.8.1'
}
//     kotlin中添加相关注解方式如下
@BindView(R.id.xxxxxx)
lateinit var view:View
//     同java,使用注解
ButterKnife.bind(this)     //     activity为例,其它类比java
  • kotlin多重嵌套向上调用
class A {
     private var m:Int = 0
     inner class B {
          private var m:Int = 1
          inner class C {
               private var m:Int = 2
               fun callFunc{
                    this.m     //     2
                    this@B.m   //     1
                    this@A.m   //     0
               }
          }
     }
}

//     关键字inner,内部类没有加inner前缀,多重内部类无法向上调用
//     适用于隐形内部类
  • kotlin隐形创建接口对象时,提示没有构造函数的问题。no construct
//     错误示例
interface iFace{
     .....
}
var fc = iFace{....}          //     interface iFace does not have constructors
//     使用方法
interface iFace{
     .....
}
var fc = object : iFace{....}          //     interface iFace does not have constructors
  • kotlin使用mutablelist可写列表
    • 与java不同,kotlin分为只读列表List和可读写列表MutableList
var readOnlyList : List<T> = emptyList()     
var readOnlyList : List<T> = listOf(t1, t2, t3, t4)   // 只读列表

var readWriteList : MutableList<T> = mutableListof()     //     可读写列表
var readWriteList : MutableList<T> = mutableListOf<T>()

坑列表会持续更新。。。。

上一篇:【Android】Android动态代理为SurfaceHolder添加Hook


下一篇:深入浅出Mesos(一):为软件定义数据中心而生的操作系统