Kotlinbase.kt

@file:JvmName("kotlinbaseTest")
import kotlin.math.roundToInt as atoInt

fun main() {


    /*val number=99
    val result= if (number in 10..59){
            "不及格"
    }else {

        "及格"
    }
    println(result)*/

    /*val week=5
    val info= when(week){
        1-> "礼拜一"
        2-> "礼拜二"
        3-> "礼拜三"
        else -> "不是礼拜"
    }
    println(info)*/

    //shuffled
    /*(0..100).shuffled().forEachIndexed{
            index,value->
        println("$index == $value")
    }*/

    /*val gardongg="皇室公园"
    val time =6

    println("今天是礼拜$time,去$gardongg 玩")*/

    //函数默认都是static final
    /*val result1=method01(22,"zhangsan")
    println(result1)*/

    /*val result2=method02(name="lisi")
    println(result2)*/

    //TODO 函数
    /*show(-1)*/

    //关键字的话可以用反引号
    /* `这是一个反引号函数`("zhangsan",18)*/

    //函数类型及隐式返回
    /*val methodAction: ()->String    //()输入,String返回类型
    methodAction={                    //函数体
        println("nihao")
        "返回值"                       //返回值必须放在最后一行
    }*/

    //有参的第二种写法
    /*val methodAction2: (i1:Int,i2:Int,i3:Int)->String={
        i1: Int, i2: Int, i3: Int ->
        println(i1)
        println(i2)
        println(i3)
        println("nihao")
        "返回值"
    }
    val result3=methodAction2(1,2,3)
    val result4=methodAction2.invoke(1,2,3)
    println(result3)*/

    //如果只有一个参数
    /* val methodAction3:(String) ->String={         // 如果只有一个传入参数,可以用 it 接收
         println("nihao")
         "param is $it "

     }*/
    //返回类型自动推断,自动推断参数列表,返回值自动推断
    /*val methodAction4={v1: Double,v2:Float,v3:Int->
         println(v1)
         println(v2)
         println(v3)
         "返回值"
     }
     val result4=methodAction4(2.1,1.8f,3)
     println(result4)*/

    /*val methodAction5={
        "返回值"
    }
    val result5=methodAction5()
    println(result5)*/

    /*val methodAction6={
        num1: Int, num2: Int->
        println("两数相加结果")
        "${num1+num2}"
    }
    val result7=methodAction6(1,2)
    println(result7)*/

    //匿名函数==lambda表达式

    //返回值是Any
    /* val methodAction8={
         num: Int->
         when(num){
             1->"星期1"
             2->"星期2"
             3->"星期3"
             4->"星期4"
             5->"星期5"
          else-> -1

         }
     }

     val result8=methodAction8(5)
     println(result8.javaClass)
     val result8temp=methodAction8(8)
     println(result8temp.javaClass)*/

    //函数中定义的参数是函数
    /* fun methodAction9(name:String,password:String,result:(String,Int)->Unit){
          if (name=="zhangsan"&&password=="123"){
              result("success",200)
          }else{
              result("fail",403)
          }
      }
      methodAction9("zhangsan","123"){
              msg:String,code:Int->
          println("message : ${msg}, code :${code}")
      }
      methodAction9("lisi","234"){
              msg:String,code:Int->
          println("message : ${msg}, code :${code}")
      }*/


    //理解一
    /*val methodAction9Temp={
        name:String,password:String,result:(String,Int)->Unit->
        if (name=="zhangsan"&&password=="123"){
            result("success",200)
        }else{
            result("fail",403)
        }
    }
    methodAction9Temp("zhangsan","123"){
            msg:String,code:Int->
        println("message : ${msg}, code :${code}")
    }*/

    //理解二,改变输入参数的返回值,匿名函数ifexpress的返回值也会跟着变
    /* val ifexpress={
         result:(String,Int)->String->
         println("dddd")
         if (true){
             result("success",200)
         }else{
             result("fail",403)
         }
     }
     val resultifexpress=ifexpress(){
             msg:String,code:Int->
         "message : ${msg}, code :${code}"
     }
     println(resultifexpress)*/



    //函数中定义的参数如果是lambda表达式可以使用内联来使代码运行效率更优,可参考methodAction10  inline表示此函数是内联函数,内联函数必须单独定义,不能写在函数中定义
    //函数参数不是函数类型的函数,无需定义为内联
    /*methodAction10("zhangsan","123"){
        msg:String,code:Int->
        println("message : ${msg}, code :${code}")
    }*/

    //函数类型引用,将函数实例传入函数调用中
    /*methodAction11("zhangsan","123",::resultFun)*/


    //函数返回值是函数
    /*fun methodAction12(): (name:String,age:Int)->String{
        println("nihao")
        return {
            name:String,age:Int->
            "my name is $name,my age is $age"
        }
    }

    val result12=methodAction12()("zhangsan",26)
    println(result12)*/

    //入参是函数的调用方式,此为匿名函数调用
    /*fun methodAction13(name:String,age:Int,result:(String)->Unit){
        result("my name is $name")
    }

    fun resultMethod(name:String){
        println(name)
    }
    //方式一
    methodAction13("zhangsan",22,{name:String-> println(name)})
    //方式二
    methodAction13("lisi",34){
            name:String-> println(name)
    }
    //方式三,具名方式调用
    methodAction13("wangwu",30,::resultMethod)
    */

    //可空性特点
    //第一种情况
    /*var name:String="zhangsan"
    name=null          //默认不能给null
    //第二种情况
    var name2:String ?    //声明时指定可为null
    name2=null
    name2="wangwu"*/

    //null 安全调用
    /* var name3:String?
     name3=null
     name3?.toString()    //当name3为null时,toString()方法不会被调用,避免空指针*/

    //let 安全调用
    /* var name4:String?=null
     name4?.let {                //如果name4为空let不执行,可以通过it获取name4的值,并且it一定不为null
         println("$it")
         "不为空"
     }
     println(name4)*/

    //非空断言
    /* var name5:String?=null
     val r=name5!!.toString()           //!!表示不管name5为不为null都执行toString(),此时会抛出空指针*/

    //if else
    /*var name6:String?=null
    if(name6!=null){
        name6.toString()
    }else{
        println("name6 is null")
    }*/

    //空合并操作
    /*var name7:String?=null
    println(name7?:"name7 is null")  //name7为null执行?:后面的语句,不为空就是name7*/

    //异常处理
    /*try {
        var name8:String?=null
        name8!!.length
    }catch (e:Exception){
        println("异常捕获")
        println(e)
    }
    */

    //checkNotNull
    /*var name9:String?=null
    //checkNotNull(name9)    //java.lang.IllegalStateException: Required value was null.
    requireNotNull(name9)    //java.lang.IllegalArgumentException: Required value was null.*/

    //Boolean cehck
    /* var isSame:Boolean= false
     require(isSame)   //java.lang.IllegalArgumentException: Failed requirement.*/

    //subString
    /*val text:String="aaaaaaaaddddddddfffffff"
    println(text.substring(0,6))
    println(text.substring(0 until 6))*/

    //split 分割
    /* val text1:String="dddd,ffff,ggg,hhhh,jj"
     val list=text1.split(",")
     val (v1,v2,v3,v4,v5)=list
     println(list)
     println("v1:$v1,v2:$v2,v3:$v3,v4:$v4,v5:$v5")
    */


    //replace提取并运算替换
    /*val password:String="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    val result=password.replace(Regex("[AFG]")){
        println("获取的字符:${it.value}")
       "*"
    }
    println(result)*/

    //==&===  ==相当于java equals,===引用比较
    /* val str1="zhangsan"
     val str2="zhangsan"
     println(str1==str2)    //true
     println(str1===str2)   //true  常量值引用是一样的
     val str3="Zhangsan"
     val str4="zhangsan".capitalize()  //str4="Zhangsan"
     println(str3===str4)   //false*/

    //字符串遍历
    /*val foreachstr="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    foreachstr.forEach { print("$it") }*/

    //安全转换函数
    /* val number : Int ="111".toInt()
     val number1 : Int? ="111.1".toIntOrNull()*/

    //Double转Int
    /*println(22.234.toInt())  //四舍五入 22
    println(22.2.roundToInt())  // 同上
    println(22.46.roundToInt())
    println("%.3f".format(22.22222))   //22.222  保留三位*/

    //apply内置函数   apply始终返回 调用者本身
    /*val info="Zhang San Ni Hao"
    val result10=info.apply {
        println(this)            //Zhang San Ni Hao
    }.apply {
        println("$length")       //16
    }.apply {
        println("${toLowerCase()}")  //zhang san ni hao
    }*/


    //let内置函数   let会返回函数体内最后一行的内容类型决定,而apply始终返回调用者本身
    /* val list= listOf(1,2,3,4,5,6)
     val result=list.let {
         println(it.size)
         it.size
     }
     println(result.javaClass)*/

    //run内置函数同let函数一样,也是根据函数体最后一行类型返回
    /*fun istrue(istr:Boolean):String{
       return  if(istr) {
            "is true"
        }else{
            "is false"
        }
    }
    val result= "1234546".run {
        println(this)
        true
    }.run (::istrue)
    println(result)*/

    //with内置函数
    /*val str="123456"
    with(str,::println)
    with(str,{
        println(this.length)
    })*/

    //also内置函数, 和apply一样
    /*val str="123456"
    str.also {
        println(it)
        true
    }.apply {
        println(this)
    }*/

    //takeif内置函数
    /*println("123456".takeIf { true })  //123456
    println("123456".takeIf { false })  //null*/

    //takeUnless内置函数  和takeif 功能相反
    /*println("123456".takeUnless { true })  //null
    println("123456".takeUnless { false })  //123456*/


    //集合框架 普通list不可变
    /* var list= listOf("1","2","3")
     list=list.plus("4")
     println(list)
     val result1=list.getOrElse(3,::println)
     val result2=list.getOrElse(4,{"out of index1"})
     println(result1)
     println(result2)
     val result3= list.getOrNull(4)?:"out of index2"
     println(result3)*/

    //可变list
    /*val list:MutableList<String> = mutableListOf()
    list.add("1")
    list.add("2")
    list.add("3")
    list.remove("1")
    println(list)    //[2, 3]
    val list1= listOf<String>().toMutableList()
    list1.add("1")
    list1.add("2")
    list1.add("3")
    println(list1)   //[1, 2, 3]*/

    //+= -=
    /*val list= mutableListOf<String>()
    list+="1"
    list+="2"
    list+="3"
    list-="1"
    println(list)  //[2, 3]
    //removeIf   当判断成立时删除某个元素
    list.removeIf {
        it.contains("2") //[3]
    }*/

    //list遍历
    /*val list= mutableListOf<String>()
    list.add("1")
    list.add("2")
    list.add("3")
    list.forEach {
        println(it)
    }
    list.forEachIndexed {
            index, s ->
            println(index)
            println(s)
    }*/


    //结构语法
    /*val(v1,v2,v3)= listOf<String>("1","2","3")
    println(v1)
    println(v2)
    println(v3)
    val(_,v4,v5)= listOf<String>("4","5","6")  //过滤掉第一个元素和go语言语法一样
    */

    //set集合不会重复  操作和 list类似
    /*val set= setOf<String>("1","2","3","1")
    println(set)   //[1, 2, 3]
    val muset=set.toMutableSet()
    muset.add("5")
    muset.add("6")*/

    //集合转换去重
    /*val list= mutableListOf<String>("1","2","3","4","1")
    val muset1=list.toMutableSet()
    println(muset1)*/

    //数组
    /**
     * IntArray intArrayOf
     * DoubleArray doubleArrayOf
     * ...
     * Array  arrayOf   对象数组
     */
    //val intArray= intArrayOf(1,2,3,4,5)

    //Map
    /* val map= mapOf<String,String>("1" to "2","3" to "4")
     val mumap=map.toMutableMap()
     println(mumap.get("1"))
     println(mumap["3"])
     mumap.forEach { t, u ->
         println(t)
         println(u)
     }
     mumap.forEach{
         println(it.key)
         println(it.value)
     }
     mumap.put("zhang","san")
     mumap += ("wang" to "wu")
     mumap -= "1"
     mumap.forEach{
         println(it.key)
         println(it.value)
     }
     mumap.getOrPut("li"){"si"}   //如果没有li 就加进去
     mumap.forEach{
         println(it.key)
         println(it.value)
     }*/


    //定义类 get set 重写
    /*class Class01{
        var name:String="zhangsan"   //var是自动会创建get set函数给相应的属性
            get(){
                println("getting")
                return "wangwu"
            }
        var age:Int=24
            set(value) {
                println("setting")
                field=value   //field关键字代表属性本身
            }
        val sex:Int=0     // // Class01().sex=1   val 只有get函数无set函数所以当给实力属性赋值时会报编译错误
    }

    val instance=Class01()
    println(instance.age)   //24
    println(instance.name)  //wangwu
    instance.age=40
    println(instance.age) //40*/

    //主构造函数
    /* class Class02(_name:String,_age:Int,_sex:Int){
         var name:String=_name   //var是自动会创建get set函数给相应的属性
             get(){
                 println("getting")
                 return field
             }
         var age:Int=_age
             set(value) {
                 println("setting")
                 field=value   //field关键字代表属性本身
             }
         var sex:Int=_sex    // // Class01().sex=1   val 只有get函数无set函数所以当给实力属性赋值时会报编译错误
     }

     val instance01=Class02("zhangenhao",30,1)
     println(instance01.age)
     println(instance01.name)
     println(instance01.sex)*/

    //一步到位定义类包括类中属性
    /*class Class03(var name:String,var age:Int,var sex:Int){
        fun show(){
            println(name)
            println(age)
            println(sex)
        }
    }
    val instance3=Class03("zhangenhao",50,1)
    println(instance3.age)
    println(instance3.name)
    println(instance3.sex)
    instance3.show()*/


    //次构造
    /* class Class04(){

         init {
             println("实力对象初始化时被调用")
         }
         var name:String=""
         var age:Int=28
         var sex:Int=1
        constructor(name:String,age:Int,sex:Int): this(){    //: this() 调用无参主构造,必须调用主构造不然会编译错误
             this.name=name
             this.age=age
             this.sex=sex
            println("此构造被调用")
        }
     }
     val instance04=Class04("zhangenhao",30,1)
     println(instance04.name)
     println(instance04.age)
     println(instance04.sex)
     //调用流程
     *//**
     *
     * 实力对象初始化时被调用
     * 此构造被调用
     * zhangenhao
     * 30
     * 1
     */

    //lateinit  延迟初始化  必须是var才能延迟初始化
    /* class Class05(){
         lateinit var name:String
         var age:Int = 0    //基本类型不能使用延迟初始化
         var sex:Int=1
         constructor(name:String,age:Int,sex:Int): this(){    //: this() 调用无参主构造,必须调用主构造不然会编译错误
             this.name=name
             this.age=age
             this.sex=sex
         }
     }
     val instance05=Class05("zhangenhao",30,1)
     println(instance05.name)
     println(instance05.age)
     println(instance05.sex)*/

    //by lazy  惰性初始化
    /* class Class06(){
         val name:String by lazy{ inityouname() }   //只能用于val 不能用于var
         var age:Int = 0    //基本类型不能使用延迟初始化
         var sex:Int=1
         constructor(name:String,age:Int,sex:Int): this(){    //: this() 调用无参主构造,必须调用主构造不然会编译错误
             this.age=age
             this.sex=sex
         }
         fun inityouname():String{
             println("loading data!!!")
             println("loading data!!!")
             println("loading data!!!")
             println("loading data!!!")
             return "lisi"

         }
     }
     val instance05=Class06("zhangenhao",30,1)
     println("准备加载!!!")
     println(instance05.name)
     println(instance05.age)
     println(instance05.sex)*/


    //继承与重载
    //kotlin中类的类型默认都是final的,所以不能继承,加上open就可以去掉final,然后子类才能继承他
    /* open class Class07(var name:String){
         var age:Int=0
         open fun myshow(){    //方法默认也是final类型不能重写,加上后去掉final就可以被重写
             println(name)
         }
     }
     class Class08(var subfix:String):Class07(subfix){   //继承
         override fun myshow() {
             println("zilei")
         }
         var youshow={
             println("youshow!!!")
         }
     }
     Class08("kotlin").myshow()
     val instance06:Class07=Class08("wangwu")
     instance06.myshow()
     println(instance06 is Class08)
     println(instance06 is Class07)
     (instance06 as Class07).myshow()  //类型转换
     println(instance06.age)
     (instance06 as Class08).youshow()   //转过一次后后面就无需再转能直接调用,不然调用不到 youshow方法 //智能类型转换
     instance06.youshow()*/

    //object 对象声明  用object声明一个单例实例
    /*println(Class08)
    println(Class08)
    Class08.show()*/

    //匿名对象
    /* open class Class09{
         open fun add()= println("add")
         open fun del()= println("add")
     }
     //匿名方式
     var instance09=object : Class09(){
         override fun add() {
             println("class09 add")
         }

         override fun del() {
             println("class09 del")
         }
     }
     instance09.add()
     instance09.del()
     //具名方式
     class Class10: Class09(){
         override fun add() {
             println("class10 add")
         }

         override fun del() {
             println("class10 del")
         }
     }
     Class10().add()
     Class10().del()*/

    //companion object 伴生对象,相当于 Java static关键字, 无论创建多少新对象 伴生对象只会初始化一次
    /* println(Class11.info)
     Class11.show()*/

    //内部类 ,
    // 内部类 能访问 外部类的所有东西
    // 外部类 能访问 内部类的所有东西
    /*class Body{
        var sex:Int=0
        fun alive(){
            Heart().jump()
        }
        inner  class Heart(){
            fun jump(){
                println(sex)
                println("jump")
            }
        }
    }
    Body().alive() */

    //数据类
    /*class Class12(var name:String,var age:Int,var sex:Int){}    //普通类只提供get set 构造函数
    data class Class13(var name:String,var age:Int,var sex:Int){}    //普通类只提供get set 构造函数 解构操作 copy toString hashcode equals
    println(Class12("zhangenhao",29,1).name)
    println(Class13("zhangenhao",29,1).name)
    println(Class13("zhangenhao",29,1).hashCode())
    println(Class13("zhangenhao",29,1).copy(sex=0).sex)
    println(Class13("zhangenhao",29,1).copy(sex=0).toString())
    val (v1:String,v2:Int,v3:Int)=Class13("zhangenhao",29,1)
    println(v1) //zhangenhao
    println(v2) //29
    println(v3) //1
    println(Class13("zhangenhao",29,1).equals(Class13("zhangenhao",29,1)))  //true*/

    //运算符重载
    /*data class Class14(var num:Int){
        operator fun plus(class14: Class14): Int {
            return num+class14.num
        }
    }
    println(Class14(1)+Class14(2))*/

    //枚举
    /*println(ManInfo.LISI.hight)*/

    //代数数据类型
    /*class Teacher (val exam: Exam){
        fun show()=
            when(exam){
                Exam.Fraction1-> "差"
                Exam.Fraction1-> "及格"
                Exam.Fraction1-> "良好"
                Exam.Fraction1-> "优秀"
                *//*else ->""*//*   //因为ENUM 类型只有四种类型,所以 else可以不用写,这种就叫做代数数据类型
                else -> "无"
            }
    }
    println(Teacher(Exam.Fraction1).show())*/

    //接口定义
    /* class Mouse():Iusb {
          override var usbVersion: String=""
              get() = field
              set(value) {
                  field=value
              }
          override var usbDevice: String=""
              get() = field
              set(value) {
                  field=value
              }

          override fun insertUsb(): String {
              return "$usbVersion == $usbDevice"
          }
      }

      val mouse=Mouse()
      mouse.usbVersion="usb3.0"
      mouse.usbDevice="server"
      println(mouse.usbDevice)
      println(mouse.usbVersion)
      val usbinstance:Iusb= Mouse()
      usbinstance.usbDevice="pad"
      println(usbinstance.usbDevice)
      println(usbinstance.insertUsb())*/

    //抽象类
    /*class MainActivity: BaseActivity(){
        override fun initView(): String {
           return "{'responsebody':{'code':200,'message':'ok'}}"
        }
    }
    MainActivity().onCreate()*/

    //定义泛型类
    /*class EchoClass<T>(val obj:T){
        fun show()= println(obj)
    }
    data class Student(var name:String,var age:Int)

    EchoClass(Student("zhangsan",28)).show()*/

    //泛型函数
    /*data class Teacher(var name:String,var age:Int)
    class EchoClass01<T>(val isR:Boolean,val obj:T){
        fun getObj01(): T? {
            return obj.takeIf { isR }
        }
    }
    println(EchoClass01(false, Teacher("zhangsan", 28)).getObj01()?.run { println(this) } ?: "is null")
    EchoClass01(true,Teacher("zhangsan",28)).getObj01()?.run { println(this)}?:"is null"*/

   //动态参数
    /*class EchoClass02<T>(vararg objects:T){
        val objs=objects
        fun showSize(){
            println(objs.size)
        }

        fun iteraterThem(vararg objz:String){
            objz.forEach {
                println(it)
            }
        }
    }
    EchoClass02("zhangsan",1,"lisi").showSize()
    EchoClass02("zhangsan",1,"lisi").iteraterThem("1","2","3")*/

    //out 协变,in 逆变, 不变
    /*val producer=object : Producer<String> {
        override fun product(): String {
            return "协变"
        }
    }
    println(producer.product())

    val consumer=object : Consumer<String>{
        override fun consume(item: String) {
            println(item)
        }
    }
    consumer.consume("逆变")

    val producerAndConsumer=object : ProducerAndConsumer<String>{
        override fun consume(item: String) {
            println(item)
        }
        override fun product(): String {
            return "hello 不变"
        }
    }*/

    //泛型 reified关键字
    /*data class Object1(var name:String,var age:Int)
    data class Object2(var name:String,var age:Int)
    class KotlinBase04<T>{
        inline fun <reified T> randomObj():T?{
            val objs= listOf<Any>(Object1("zhangsan",19),Object2("lisi",20))
            val randomObj=objs.shuffled().first()
            return randomObj.takeIf { it is T } as T ?: null
        }
    }
    println(KotlinBase04<Object1>().randomObj())*/

    //kotlin扩展函数学习,很恐怖的功能,很傻很强大
    /*class KotlinBase05(var name:String,var age:Int)
    fun KotlinBase05.show(){
        println(this.age)
        println(name)
        println("扩展KotlinBase05 类的方法")
    }
    KotlinBase05("zhangenhao",30).show()

    //对超类扩展
    fun Any.showPrintlnContent(){
        println(this.toString())
    }
    KotlinBase05("zhangenhao",30).showPrintlnContent()

    //对泛型进行扩展,相当于对Any进行扩展
    fun <T> T.showContent(){ 
        println("对泛型扩展")
    }
    KotlinBase05("zhangenhao",30).showContent()
    "123456".showContent()*/

    //重命名
   /* fun randomItemValuePrintLn(){
        22.2.atoInt()
    }*/

    //DSL (domain specified language)  领域专用语言  ?需要在研究

    //map函数
    /*val list= listOf("zhangsan","lisi","wangwu")
    val list1=list.map {
        println(it)
        "*$it*"
    }
    println(list1)*/    //[*zhangsan*, *lisi*, *wangwu*]

    //flatMap
    /*val list2= listOf("zhangsan","lisi","wangwu")
    val list3=list2.flatMap {
        println(it)
        listOf(it,it.length)   //返回一个集合
    }
    println(list3)   //[zhangsan, 8, lisi, 4, wangwu, 6]
    */

    //filter 过滤
    /*val namelists= listOf(
            listOf("zhangsan","男"),
            listOf("lisi","男"),
            listOf("ruhua","不男不女")
        )
    val list4=namelists.filter {
        it.contains("男")
    }
    println(list4)*/

    //zip合并
    /*val names= listOf("liudehua","liying","ruhua")
    val sexs= listOf("男","女","不男不女")
    val list5=names.zip(sexs)
    println(list5)   //[(zhangsan, 男), (lisi, 女), (wangwu, 不男不女)]
    println("************")
    list5.forEach {
        println(it.first)
        println(it.second)
        println("************")
    }
    list5.toMap().forEach {
        key,value->
        println(key)
        println(value)
    }*/

    //单例模式  ?再研究

    //注解使用
    //@JvmName 注解   //定义该kt转成Java后的类名

    //@JvmField 注解

    //@JvmOverloads 注解














}
class Class13{
    companion object{
        val info="zhangsan"
        fun show(){
            println("companion")
        }
    }
}
class Class12{
    companion object{
        @JvmStatic
        val info="zhangsan"
        @JvmStatic
        fun show(){
            println("companion")
        }
    }
}

@JvmOverloads
fun showInfo(name:String,age:Int=20,sex:Int=1){
    println("name : $name , age: $age, sex:$sex")
}


class Person(){
    @JvmField               //编译时会将字段变成public,getName方法也会被删除
    var name:String="dashen"

}
//不变  既能做为入参也能作为返回 所以这就是不变的含义
interface ProducerAndConsumer<T>{
    fun consume(item:T)
    fun product():T
}

//协变
interface Producer<out T>{
    //fun consume(item:T)   //会报编译错误,意思是T只能作为返回参数不能作为入参

    //只能作为返回
    fun product():T     //这样就是ok的  这就是out的含义

}

//逆变
interface Consumer<in T>{
    fun consume(item:T)   //这样就是ok的  这就是in的含义,意思是T只能作为入参数不能作为返回

    //只能作为返回
    //fun product():T     // //会报编译错误,意思是T只能作为入参数不能作为返回
}



abstract class BaseActivity{

    fun onCreate(){
        printView(initView())
    }
    private fun printView(view:String){
        println(view)
    }
    abstract fun initView():String
}

interface Iusb{
    var usbVersion:String
    var usbDevice:String
    fun insertUsb():String
}

enum class Exam(){
    Fraction1,
    Fraction2,
    Fraction3,
    Fraction4,
    ;
}
enum class ManInfo(var hight:Int){
    ZHANGSAN(160),
    LISI(170),
    WANGWU(180)
}
class Class11{
    companion object{
        val info="zhangsan"
        fun show(){
            println("companion")
        }
    }
}

object Class08{
    var name:String="zhangsan"

    fun show(){
        println("nihao")
    }

}

private fun method01(age: Int, name: String) : Int{
    return 200
}

private fun method02(age: Int=30, name: String="wangwu") : Int{
    println("name : $name")
    return 300
}



private fun method02(): Unit{
    println("无返回值类型,unit可省略不写")
}



private fun show(number: Int){
    when(number){
        -1-> TODO("没有这个分数")
        in 0..59-> println("不及格")
        in 59..100-> println("及格")
    }
}

private fun `这是一个反引号函数`(name:String, age:Int){
    println("name: ${name},age: $age")

}

inline fun methodAction10(name:String,password:String,result:(String,Int)->Unit){
    if (name=="zhangsan"&&password=="123"){
        result("success",200)
    }else{
        result("fail",403)
    }
}

inline fun methodAction11(name:String,password:String,result:(String,Int)->Unit) {
    if (name == "zhangsan" && password == "123") {
        result("success", 200)
    } else {
        result("fail", 403)
    }
}
fun resultFun(message:String,code:Int){
    println("message : ${message}, code :${code}")
}
JavaInvoke.java
public class JavaInvoke {
    public static void main(String[] args) {
        //@JvmName 注解
        kotlinbaseTest.resultFun("ZHANGSAN",20);

        //@JvmField 注解
        Person Person=new Person();
        System.out.println(Person.name);

        //@JvmOverloads 注解
        kotlinbaseTest.showInfo("zhangsan");  //不加该注解需要将参数全部传如,加了后就和kotlin调用一样了 可以只传第一个参数,后面的参数都是默认的

        //@JvmStatic 注解
        Class12.show();
        Class13.Companion.show();  //不加的话,必须通过Class12.Companion.show()的方式调用


    }
}

 

posted on 2022-12-12 10:50  EZgod  阅读(23)  评论(0编辑  收藏  举报