Programming language evolves always along with Compiler's evolvement The Semantics of Data The size of an empty base class or an empty derived class inherited from an empty base class is not 0. For some reason: virtual pointer to virtual function tab
fun main(args:Array<String>):Unit{ val x = a{ aa{ +"01.01" +"01.02" } aa{ +"02.01" +"02.02" } } print(x) } fun a(p:A.()->Unit):A { val a = A() a.p() return a; } class A { val children=arrayListOf<AA>(
委托模式是软件设计模式中的一项基本技巧.在委托模式中,有两个对象参与处理同一个请求,接受请求的对象将请求委托给另一个对象来处理. Kotlin 直接支持委托模式,更加优雅,简洁.Kotlin 通过关键字 by 实现委托. 类委托 类的委托即一个类中定义的方法实际是调用另一个类的对象的方法来实现的. 以下实例中派生类 Derived 继承了接口 Base 所有方法,并且委托一个传入的 Base 类的对象来执行这些方法. // 创建接口 interface Base { fun print() }
运算符重载(Operator overloading) 一元运算符 Expression Translated to +a a.unaryPlus() -a a.unaryMinus() !a a.not() data class Point(val x: Int, val y: Int) operator fun Point.unaryMinus() = Point(-x, -y) val point = Point(10, 20) println(-point) // prints "(-1