/** 
* 数据类 
*/
data class Leaf(val size:String,val color:String,val shape:String,val vein:Int) 

fun main(args: Array<String>) {    
    val myleaf = Leaf("30","green","circle",57)    
    val lsize = myleaf.size    
    val lcolor = myleaf.color    
    val lshape = myleaf.shape    
    val lvein = myleaf.vein    
    println("大小:${lsize}  颜色:${lcolor}   形状:${lshape}  叶脉数:${lvein}")     
    val myleaf2 = Leaf("30","green","circle",30)    
    val (size,color,shape,vein) = myleaf2    
    println("大小:${size}  颜色:${color}   形状:${shape}  叶脉数:${vein}")}

/** 
* 密封类 
*
/open class Fruit() 
class Apple:Fruit(){    
    fun operate() = println("给妈妈吃")
} 
class Banana:Fruit(){    
    fun operate() = println("给爸爸吃")
}
class WaterMelon:Fruit(){    
    fun operate() = println("自己吃")
} 
fun operate(fruit: Fruit) = when (fruit){    
    is Apple    ->      fruit.operate()    
    is Banana   ->      fruit.operate()    
    is WaterMelon->     fruit.operate()    
    else        ->      null
} 
//密封类实现上面demo
sealed class Fruit2{    
    class Apple: Fruit2(){        
        fun operate() = println("给妈妈吃")    
    }    
    class Banana:Fruit(){        
        fun operate() = println("给爸爸吃")    
    }     
    class WaterMelon:Fruit(){        
        fun operate() = println("自己吃")    
    }
}
fun main(args: Array<String>) {    
    val one_fruit = Banana()    
    operate(one_fruit)     
    val two_fruit = Fruit2.Banana()    
    operate(one_fruit)
}

/** 
* 泛型 
*/
class Container<T>(thing: T){    
    val thing: T = thing    

    fun printInformation() = println("This thing is ${thing}")
} 

fun <T>one_int_container_say_hello(c:Container<T>){    
    c.printInformation()
} 
//泛型接口
interface Factory<T>{    
    fun produce(ele:T)
}
class StringFactory:Factory<String>{    
    override fun produce(ele: String) {        
        println("produce ${ele}")    
    }
}
class IntFactory:Factory<Int>{    
    override fun produce(ele: Int) {        
        println("produce ${ele}")    
    }
}
class DoubleFactory:Factory<Double>{    
    override fun produce(ele: Double) {        
        println("produce ${ele}")    
    }
}
class MyFactory<T>:Factory<T>{    
    override fun produce(ele: T) {        
        println("produce ${ele}")    
    }
} 
interface Factory2<out T>{    
    fun produce():T
}
fun change(f:Factory2<String>){    
    val facotry_any : Factory2<Any> = f
}  

//计算器demo
interface ComputionInterface<T>{    
    fun add(num1:T,num2:T)    
    fun sub(num1:T,num2:T)    
    fun mul(num1:T,num2:T)    
    fun div(num1:T,num2:T)
}
class Compution<T>:ComputionInterface<T>{    
    override fun add(num1: T, num2: T) {
        val num = num1 + num2        
        println(num)    
    }     
    override fun sub(num1: T, num2: T) {
        val num = num1 - num2
        println(num)    
    }     
    override fun mul(num1: T, num2: T) {
        val num = num1 * num2
        println(num)    
    }     
    override fun div(num1: T, num2: T) {
        al num = num1 / num2
        println(num)    
    }
} 
fun main(args: Array<String>) {    
    val c = Container<Int>(1)    
    c.printInformation()     
    val c1 = Container(1)    
    one_int_container_say_hello(c1)    
    val c2 = Container("hi")    
    one_int_container_say_hello(c2)
}

/** 
* 嵌套类 
*/
class Taohuan{    
    val outcolor:String = "red"    
    val size:Int = 20    
    val shape:String = "circle"     

    fun getshape():String{        
        val shape = [email protected]        
        return shape    
    }     
    fun getshape2():String{        
        val shape2 = this.shape        
        return shape2    
    }     
    inner class jiangpin{        
        val incolor:String = "blue"        
        val size:Int = 15        
        val shape:String = "rect"         
        
        fun getoutsize():Int{            
            val outsize = [email protected]            
            return outsize        
        }         
        fun getinsize():Int{            
            val insize = [email protected]            
            return insize        
        }         
        fun getthissize():Int{                
            val thissize = this.size            
            return thissize        
        }    
    }
}
fun main(args: Array<String>) {    
    val mytaohuan = Taohuan()    
    val myjiangpin = Taohuan().jiangpin()    
    println("外部类this@类名  : ${mytaohuan.getshape()}")
    //circle    
    println("外部类this       :  ${mytaohuan.getshape2()}")
    //circle    
    println("内部类this@外部类名: ${myjiangpin.getoutsize()}")//20    
    println("内部类this@内部类名:${myjiangpin.getinsize()}")//15    
    println("内部类this        :   ${myjiangpin.getthissize()}")//15
}

/** 
* 枚举类 
*/
enum class Enumclass{    
    enum1,enum2,enum3;    

    fun functionA():Unit{        
        println("枚举类的常量可以直接调用类里的方法")    
    }
} 
enum class Familier(val rank:Int){    
    father(1),mother(2),son(3),daughter(4)
} 
enum class F  
enum class Familier2(var rank: Int){    
    father(1),mother(3),son(2),daughter(4);     

    override fun toString(): String {        
        var str:String = "成员名称:"+this.name + "成员排名:"+rank + "\n"
        return str    
    }    
    fun favoritefood():Unit{        
        when (this.name){            
            "father"        ->      println("father likes apple \n")            
            "mother"        ->      println("mother likes banana \n")            
            "son"           ->      println("son likes watermelon \n")            
            "daughter"      ->      println("daughter likes peach \n")            
            else            ->      println("Familier2 do not have this person \n")        
        }    
    }    
    fun action(){        
        println("wo all like sports! \n")    
    }
}
fun main(args: Array<String>) {    
    Enumclass.enum1.functionA()    
    println("话语权:\n父亲:${Familier.father.rank}  母亲:${Familier.mother.rank}"+"儿子:${Familier.son.rank}  女儿:${Familier.daughter.rank}")     
    var a:Int = Familier.mother.compareTo(Familier.son)    
    println("前者先声明,则返回 $a \n")                      //前者先声明,则返回-1    
    var b:Int = Familier.daughter.compareTo(Familier.son)    
    println("前者后声明,则返回 $b \n")                      //前者后声明,则返回1    
    var c:Int = Familier.son.compareTo(Familier.son)    
    println("两者相同则返回 $c \n")                        //两者相同返回0    
    var d:Int = Familier.father.ordinal    
    var e:Int = Familier.daughter.ordinal    
    println("ordinal()函数从 $d 开始,到 $e 结束 \n")        //ordinal()函数从0开始,到3结束      
    println("${Familier2.father.toString()}")    
    Familier2.father.favoritefood()    
    println("${Familier2.mother.toString()}")    
    Familier2.mother.favoritefood()    
    println("${Familier2.son.toString()}")    
    Familier2.son.favoritefood()    
    println("${Familier2.daughter.toString()}")    
    Familier2.daughter.favoritefood()    
    Familier2.daughter.action() 
}

/** 
* 对象 
*/
open class One(age:Int){    
    var age: Int = age
}
class Two:One(age = 10){    
    fun newaddfunction(){        
        println("我是新增的成员!")    
    }
}  
interface Three{    
    fun interfunction()
}
open class One2(age:Int){    
    open var age:Int = age    

    open fun classfunction(){        
        println("我是类里的方法")    
    }
}
fun main(args: Array<String>) {    
    var two = Two()    
    println("${two.age} \n")    
    two.newaddfunction()     
    var three = object :One(20){        
        fun newaddfunction(){            
            println("我是新增加的方法!")        
        }    
    }    
    println("${three.age} \n")    
    three.newaddfunction()     
    var two2 = object : One2(20),Three {   
     
        override fun interfunction() {            
            println("在对象表达式中覆盖了接口的方法")        
        }         

        override var age: Int = 21        

        override fun classfunction() {            
            println("在对象表达式中覆盖了父类的方法")        
        }    
    }    
    println("覆盖了父类的age属性:${two2.age}")    
    two2.interfunction()    
    two2.classfunction()
}

/** 
* 委托 
*/
interface Shangji{    
    fun getTask()
}
class Me(name:String):Shangji{    
    var name = name    

    override fun getTask() {        
        println("上级布置的任务给${this.name},他要委托给别人")    
    }
}
class AnotherOne(one:Shangji):Shangji by one  enum class Familier1(){    
    father1,mother1,son1;    

    fun favoritefood(){        
        when (this.name){            
            "father1"   ->  println("father1 likes apple")            
            "mother1"   ->  println("mother1 likes banana")            
            "son1"      ->  println("son1 likes watermelon")            
            else        ->  println("Familier1 do not have this person")        
        }    
    }
} 
interface Shangji2{    
    fun getTask()
}
class Me2(name:String):Shangji2{    
    var name = name    

    override fun getTask() {        
        println("帮忙写个程序")    
    }
}
class AnotherOne2(one: Shangji2):Shangji2 by one{    

    override fun getTask() {        
        println("你这个太low了,我给你写个高大上的")    
    }
}  
//委托属性
class Queself(mynameSX:String){    
    var mynameSX:String = mynameSX    
    var weituoSx:String by AnotherClass()
}
class AnotherClass{    
    operator fun getValue(duixiang:Any?,myshuxing:KProperty<*>):String{        
        return "$duixiang 委托了 ${myshuxing.name} 属性给 ${this@AnotherClass}"    
    }    
    operator fun setValue(duixiang: Any?,myshuxing: KProperty<*>,value:String){        
        println("$duixiang 的 ${myshuxing.name} 属性被我赋值为 $value")    
    }
} 
val LazySX:String by lazy {    
    print("表达式")    
    "结果1"
}
class Watchable{    
    var value: String by Delegates.observable("初始值"){        
        prop,old,new        ->        println("旧值:$old    -> 新值:$new")    
    }
}  
class Count{    
    var num: Int by Delegates.observable(1){        
        prop,old,new ->        var number = old * new        
        println("简单算术:$old * $new = $number")    
    }
}
fun main(args: Array<String>) {    
    val one = Me("小明")    
    AnotherOne(one).getTask()     
    Familier1.father1.favoritefood()    
    Familier1.mother1.favoritefood()    
    Familier1.son1.favoritefood()     
    var one2 = Me2("小明")    
    AnotherOne2(one2).getTask()     //委托属性    
    val example = Queself("小红")    
    println("未被委托的属性:${example.mynameSX}")//未被委托的属性:小红    
    println(example.weituoSx)                   //Quesrlf@4783da3f委托weituoSX属性给AnotherClass@378fdlac    
    example.weituoSx = "123456"                 //Quesrlf@4783da3f的weituoSX属性被我赋值为123456    
    println(example.weituoSx)                   //Quesrlf@4783da3f委托了weituoSX属性给AnotherClass@378fdlac      
    var k = 1    
    while (k<5){        
        println("${LazySX}")        
        k++    
    }     
    val exampl = Watchable()    
    exampl.value = "给一个新值"    
    exampl.value = "给另一个新值"     
    val exam = Count()    
    var i = 2    
    while (i < 10){        
        exam.num = i        
        i++    
    }
}
Copy the code