12.09

内联函数let with run apply also

//闭包形式返回
    //let定义一下作用域,判空处理
    haha.let {
        haha.run()
    }
    //类多个方法或属性,防止类多写 ,多用于onBinderViewHolder中
    with(HaHa("vv",30)){
        println("$name$age")
    }
    //let+with属性,返回最后一句话
    HaHa("vv",30).run {
        println("$name$age")
    }
    //返回this
    //let+with属性,返回对象自身,用于实例初始化或 view = View.inflate().apply{初始化}
    val haHa = HaHa("vv",30).apply {
        println("$name$age")
    }
    //also 与let一样,不过返回的是对象本身
    val haHa2 = HaHa("vv",30).also {
        println("$it.name$it.age")
    }

静态常量

companion object{
    val foo:String = "aa"
    const val foo2:String = "bb"
    @JvmField
    val foo3 = "cc"
}

顶级常量:const val FOO = "foo"

集合数组常用操作

image.png

循环跳出

image.png

类型强转

image.png

类基础

//构造函数
//无(注释,可见性修饰符)constructor可省略
class SuperClass @JvmOverloads private constructor(name:String, age:Int = 0){}
class SuperClass1(name:String){
//    var j = SuperClass("jj")//JvmOverloads自动生成2个构造函数
}
//定义成员变量,演变1
class SuperClass2 (name:String,age:Int){
    private var name:String
    private var age:Int
    init {
        this.name = name
        this.age = age
    }
}
//演变2
class SuperClass3 (name:String,age:Int){
    private var name:String = name
    private var age:Int = age
}
//演变3
open class SuperClass4 (private var name: String, private var age: Int){
    constructor(name: String,age: Int,url:String):this(name,age){
    }
    constructor(name: String,age: Int,url:String,sex:Boolean):this(name,age,url){
    }
    //默认类中所有是final的,想覆写或继承必须open
    open fun haHa(){
        this.name = "你好"//this关键字用于访问对象的其他成员
        this.age = 90
    }
    fun hh(){}
}

class ChildClass constructor(private var name: String,private var age: Int,private var url: String) : SuperClass4(name,age,url,true){
    override fun haHa() {
        super.haHa()
        super.hh()
        this.url = "http://www.baidu.com"
    }
}
//抽象类的abstract修饰符默认是open的
abstract class A{
    abstract fun jj()
    fun ii(i:Int){}
}

class B(private var age:Int) : A(){
    override fun jj() {
        super.ii(age)
    }
}
//接口
interface C{
    fun c()
}
interface D{
    fun d()
}
class E(food:String):C,D{
    override fun c() {}
    override fun d() {}
}
//嵌套类,内部类
class Outer{
    var name = "sdfa"
    var jj = Nested().exe()
    class Nested{
        fun exe(){
            //访问不了外部类的成员
        }
    }

}
class Outer2{
    var name = "saf"
    inner class Inner{
        fun exe(){
            println(name)
        }
    }
}
fun uu(){
    Outer2().Inner().exe()
}
//枚举,枚举类实例可数,密封类子类可数
enum class Week{
    星期一,星期二,星期三,星期四,星期五,星期六,星期日
}
enum class Week2(var arg1:String,var arg2:String){
    YI("22","ee"),
    ER("44","gg")
}
fun dd(){
    Week2.YI.arg1 = "555"
}
var jj  = Week2.YI.arg1

//密封类构造函数是私有的,因此密封类的子类必须在内部或同一文件内
sealed class HHH{
    object AAA : HHH() {}
    class BBB: HHH(){}
}
class CCC:HHH(){
    fun hh(){}
}
//数据类,必须val或var修饰,主构造至少一个参数
//此类编译器可以自动生成equals,hashcode等方法
data class Bean(val hhh: HHH){}
//单例
object Singleton{
    var name = "单例"
    fun hj(){

    }
}
fun call(){
    Singleton.name = "111"
    Singleton.hj()
}

//伴生对象,kotlin中的静态变量,类加载时初始化,和类生命周期一样
class ddd{
    companion object 伴生对象名称{//(也可以不写名称)
        fun ooo(){}
    }
}
fun ttt(){
    ddd.伴生对象名称.ooo()

}
//委托代理模式 AA委托给JJJ
interface AA{
    fun aa()
}
class JJJ:AA{
    override fun aa() {

    }
}

//一
class P:AA by JJJ(){}
class PP(jjj: JJJ):AA by jjj

fun uuu(){
    //一
    var p:P = P()
    p.aa()
    //
    var jjj =JJJ()
    PP(jjj).aa()
}
//属性委托,child委托给parent,也就是,委托类监听被委托类
class Parent{
    var name:Int = 0
    //setValue() 和 getValue() 方法前必须用 operator 关键字修饰
    //
    //getValue()方法返回类型必须与委托属性相同或是其子类
    //
    //如果委托属性是 val 类型,被委托方法只需要实现 getValue(), 如果是 var 类型,则需要实现
    //getValue() 和 setValue()
    operator fun getValue(child: Child, property: KProperty<*>): Int {
        println("getValue")
        return name
    }

    operator fun setValue(child: Child, property: KProperty<*>, i: Int) {
        println("setValue")

    }
}
class Child{
    var name:Int by Parent()
    fun ooo(){
        println("被委托类的方法")
    }
}
fun LLL(){
    val child:Child = Child()
    child.name=100

}
//延迟加载,变量被访问时才会初始化,val ** by lazy
//执行第一句话
//执行中间一句话
//执行最后一句//初始化完
//safsdfsf//开始执行,只会执行最后一句
//safsdfsf
//safsdfsf
fun kkk(){
    val name by lazy {
        println("执行第一句话")
        println("执行中间一句话")
        println("执行最后一句")
        "safsdfsf"
    }
    println(name)
    println(name)
    println(name)
}

你可能感兴趣的:(12.09)