kotlin第二篇

第一次转换java代码为kotlin,写一下kotlin没了解到的地方吧

  • 字符串的长度
var s:String
s!!.trim { it <= ' ' }.length
  • 构造方法实现接口?必须唯一化?
client.newCall(request).enqueue(object : Callback {
                override fun onFailure(call: Call, e: IOException) {
                    e.printStackTrace()
                }

                @Throws(IOException::class)
                override fun onResponse(call: Call, response: Response) {

                }
            })
  • 下面这样只能set不能set
var stu_per_schedules = HashMap<String, ArrayList<String>>()
        private set
  • 一个变量的get方法可以这样写
val dataWhenLoginSuccess: String
        get() = htmlData!!.toString()
  • 下面这样我感觉得意思就是内部静态吧?
companion object {

        private var tochange = false
    }
  • 因为kotlin中的类定义同时也是构造函数,这个时候是不能进行操作的,所以kotlin增加了一个新的关键字init用来处理类的初始化问题,init模块中的内容可以直接使用构造函数的参数。
inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var score_Subject_name: TextView
        init {            
            score_Subject_name = itemView.findViewById(R.id.score_Subject_name)
        }
    }


class Mydapter(private val context: Context) : RecyclerView.Adapter<Mydapter.MyViewHolder>() {

    private val context: Context
    init {
        this.context= context
    }
}

猜你喜欢

转载自blog.csdn.net/asffghfgfghfg1556/article/details/79404935