美文网首页
kotlin 学习

kotlin 学习

作者: alex_zn | 来源:发表于2018-07-20 17:10 被阅读0次

    基础

    class Person{
        private var clo:(()->Unit)? = null
        public var name:String? = null
    }
    //扩展属性
    val Person.kinds:Int
    get() = 1
    
    //扩展函数
    fun Person.desc(){
    
        println("扩展函数")
    }
    enum class Direction {
        North,South,West,East
    }
    //lamada
    fun testClosure(closure: ()->Unit) {
    
        closure()
    }
    
    
    1,  Double  64 ,Float   32, Long    64,Int  32,Short    16,Byte,
    2,  break 和 continue 标签
        loop@ for (i in 1..100) {
            for (j in i..100) {
                if (...)
                    break@loop
            }
        }
    3, 构造函数
        1,class Customer(name: String) {} //一级
        2,class Person { // 二级
                constructor(parent: Person) {
                    parent.children.add(this)
                }
            } 
    4, 接口重写
            interface A {
                fun foo() { print("A") }
                fun bar()
            }
            
            interface B {
                fun foo() { print("B") }
                fun bar() { print("bar") }
            }
            
            class C : A {
                override fun bar() { print("bar") }
            }
            
            class D : A, B {
                override fun foo() {
                    super<A>.foo()
                    super<B>.foo()
                }
            }
            
    4, 嵌套类
        class Outer {
            private val bar: Int = 1
            class Nested {
                fun foo() = 2
            }
        内部类
            class Outer {
                private val bar: Int = 1
                inner class Inner {
                    fun foo() = bar
            }
    
    

    延伸

    class LearnOne {
    
        // 函数声明
        fun sum(a:Int, b:Int):Int {
    
            println(a+b)
            return a+b
        }
        // 自推导型的返回值
        fun sum1(a: Int, b: Int) = a + b
    
        // 返回无意义值 Unit, $ 获取参数值
        fun printSum(a: Int, b: Int): Unit {
            println("sum of $a and $b is ${a + b}")
        }
        // 字符串模版
        fun str(args:Array<String>) {
    
            var a = 1
            val s1 = "a is $a"
            var s2 = "args[1] is ${args[0]}"
            println(s1)
        }
        // 可空值
        fun parseInt(str: String?): Int? {
    
            var valueInt = str?.toInt()
            return valueInt
        }
        // 高阶函数
        fun <T> locked(lock:Lock,v1:T,v2:T,body:(a:Int)->T):Boolean where T:Comparable<T>{
    
            lock.lock()
            val a = body(100)
    
            println(a)
            return  v1 > v2
        }
    
    
        // 测试
        fun test() {
    
            val lock = ReentrantLock()
    
            var l1 = locked(lock,1,2, { a:Int ->
    
                1000
            })
            print(l1)
    
        }
    }
    

    相关文章

      网友评论

          本文标题:kotlin 学习

          本文链接:https://www.haomeiwen.com/subject/zvpzpftx.html