kotlin基础

xiaoxiao2021-02-28  134

阅读Programming Kotlin 敲的代码 作个记录 只是给自己看的

import java.io.File import java.math.BigDecimal import java.util.* /** * Created by Administrator on 2017/6/3. */ val name = "kotlin"; //var name1; //name1 = "kotiln"; //var name2 = ""; //name2 = "dd"; fun plusOne(x:Int) = x+1; val expliciType : Number = 12.3; val aToZ = "a" .. "z"; val oneToNine = 1..9; val isTrue = "c" in aToZ; val isFalse = 11 in oneToNine; val countingDown = 100.downTo(0); val rangeTo = 10.rangeTo(20); val oneToFifity = 1..50 val oddNumbers = oneToFifity.step(2) val test = (1..40).reversed() fun test1(){ val list = listOf(1,2,3,4) for(k in list){ println(k) } val set = setOf(1,2,3,4); for(k in set){ println(k) } val onToTen = 1..10; for(k in onToTen){ for(j in 1..5){ println(k*j) } } val string = "print my characters" for(char in string){ println(char) } for(index in string.indices){ println("Element $index is ${string[index]}") } } fun test2(){ val file = File(""); val date = BigDecimal(100) //=== == val date1 = Date(); val today = if(date1.year ==2016) true else false; fun isZero(x : Int):Boolean{ return if( x== 0) true else false; } } fun isString(any : Any):Boolean{ return if( any is String) true else false; } fun printStringLength(any : Any){ if(any is String){ println(any.length) } } fun isEmptyString(any : Any):Boolean{ return any is String && any.length == 0; } fun isNotStringOrEmpty(any : Any):Boolean{ return any !is String || any.length == 0 } fun returnnull(){ val any = "/home/" val string:String ? = any as String; // 强转失败不会抛castClassException 返回空指针 因为? val file : File? = any as File; } fun whentest(){ fun whatNumber(x : Int){ when(x){ 0 -> println() 1 -> println(); else -> println(); } } fun isMinOrMax(x : Int) : Boolean{ val isZero = when(x){ Int.MIN_VALUE -> true Int.MAX_VALUE -> true else -> false } return isZero } fun isZeroOrOne(x : Int):Boolean{ return when(x){ 0,1 -> true else -> false } } fun isAbs(x : Int):Boolean{ return when(x){ Math.abs(x) -> true else -> false } } fun isSingleDigit(x : Int): Boolean{ return when(x){ in -9..9 -> true else -> false } } fun isDieNumber(x : Int) : Boolean{ return when(x){ in listOf(1,2,3,4,5,6) -> true else -> false } } fun startsWithFoo(any : Any):Boolean { return when(any) { is String -> any.startsWith("Foo") else -> false } } fun whenWithoutArgs(x:Int , y : Int){ when{ x < y -> println() x > y -> println() else -> println() } } } fun printLessThanTwo(){//return 不会结束掉循环语句 val list = listOf(1,2,3,4) list.forEach(fun(x){ if(x < 2) println(x) else return }) println("this linewill still execute") } fun printUnitilStop(){ val list = listOf("a","b","stop","c") list.forEach stop@{ if(it == "stop") return@stop else println(it) } } fun printUnitilStop1(){ val list = listOf("a","b","stop","c") list.forEach { if(it == "stop") return@forEach else println(it) } } fun printUnitilStop2(){ val list = listOf("a","b","stop","c") list.forEach gac@{ if(it == "stop") return@gac else println(it) } }
转载请注明原文地址: https://www.6miu.com/read-25084.html

最新回复(0)