Kotlin简单使用

Kotlin
1.新建一个class
private var name: String? = null
private var member: Int= 1000
private val lamube:(a:Int,b:Int)->Int={x,y ->x+y}
private var b: Double= 1.0001
private var c: Float= 1.000f
private var d: Long= 100L
get() = field
set(value) {
field=value
}
private var e: Byte= 5
private var m: Int=0b01
private var n:Int=0b00
private var g:Int=0b10
private var age: Byte= 0
fun sum(vararg a:Int){
var sum:Int=0
for (x in a){
sum+=x
}
println(“所传参数”);
}
fun setNmae(s: String?){
name = s
var leng:Int?=name?.length
var bcd:String=“abcddd”
// Log.e(“打印日志”,“leng的值KaTeX parse error: Expected '}', got 'EOF' at end of input: ….e("打印日志","d的值是i”)
// println(“d的值$i”);
// }

    //10-1数  downTo倒序  until正序

// for (i in 10 downTo 1 step 2){
// println(“d的值$i”)
// }

    //shl 左 shr 右
    var t:Int=g shr  1
    println("t的值$t")

    //or 对应的相加  and对应的相乘
    var h:Int=m or g
    println("t的值$h")
    var stringArray:Array = arrayOf("哈哈哈","呵呵")
    stringArray.forEach {
        println("数组的值$it")
    }
    var intArray:Array = Array(6,{a:Int->a*5})
    intArray.forEach {
        println("int数组的值$it")
    }

    var IntArray:IntArray = intArrayOf(1,2,9)
    stringArray.forEach {
        println("数组的值$it")
    }

    //?.判断是否有值  !!.必须有值
    var age:String=""
    var agePlus:Int=age?.toInt()
    var age123:Int=age!!.toInt()
    for ((key,value) in intArray.withIndex()){
        println("int数组的下标$key 值是$value")
    }

    val  a:Int=1000

// println(a===a)

    val boxa:Int?=a
    val boxb:Int?=a
    println(boxa == boxb)
    println(boxa === boxb)

    var list:List = listOf("小迪","地妹妹")
    for (temp in list){
        println("内容是$temp")
    }
    var interlist = list.listIterator()
    while (interlist.hasNext()){
        println("内容是${interlist.next()}")
    }

    var setMutble: MutableList = MutableList(5,{a :Int->"$a"})
    setMutble.add("嗯嗯呢")
    setMutble.add("痕痕")
    for (temp in setMutble){
        println("内容是 $temp")
    }
    setMutble.clear()
    println("size是 ${setMutble.size}")

    var map:Map = mapOf(Pair("1",0),Pair("2",1),Pair("3",2))
    for (item in map){
        println("数值是 ${item.key} 数字${item.value}")
    }

    var mutiblemap:MutableMap = mutableMapOf()
    mutiblemap.put("1","xxx")
    mutiblemap.put("2","vvv")
    mutiblemap.put("3","mmm")
    for (item in mutiblemap){
        println("数值是 ${item.key} 数字${item.value}")
    }
    //set无序
    var  mySet:Set = setOf("100","200","300")
    for (item in mySet){
        println("数值是 $item ")
    }
    var mutabSet:MutableSet = mutableSetOf()
    mutabSet.add("1");
    mutabSet.add("2");
    mutabSet.add("3");
    for (item in mutabSet){
        println("set 是 $item ")
    }

    //迭代器
    var interpolator = mutabSet.iterator()
    while (interpolator.hasNext()){
        println("set 是 ${interpolator.next()} ")
    }

    lable@
    for (item in mutiblemap){
        for (tempItem in mutabSet){
            if (tempItem.equals("2")){
                continue
            }else{
                println("内循环是 ${tempItem} ")
            }
        }
    }
}

2.在主类调用
  var  my:MyFirstClass=MyFirstClass()
my.setNmae("hfsiu")
3.测试类
var temp:MyFirstClass= MyFirstClass();
temp.setNmae("ddddd")

你可能感兴趣的:(andrio)