const val FINAL_VALUE: String = "我不可修改"
var helloWorld: String = FINAL_VALUE
//可以不用写类型,编译器会识别出来
val FINAL_MARIO = "马里奥"
fun main(args: Array<String>) {
println(helloWorld)
helloWorld = FINAL_MARIO
println(helloWorld)
}
fun sum(arg1: Int, arg2: Int) = arg1 + arg2
fun check(args: Array<String>) {
if (args.size < 2) {
println("参数至少为2个")
// System.exit(0)
System.exit(-1) //255
}
}
fun main(args: Array<String>) {
check(args)
val i = args[0].toInt()
val j = args[1].toInt()
println("$i + $j = ${sum(i, j)}")
}
把匿名函数赋值给变量
val int2Long = fun (i: Int) = i.toLong()
fun main(args: Array<String>) {
println(int2Long(3))
}
没有返回值 Unit 相当于java的 void
**
val sum = {
arg1: Int, arg2: Int ->
println("您输入的数字分别是 $arg1 和 $arg2")
arg1 + arg2 //最后一行 即 返回值
} //(Int,Int) -> Int
fun printUsage() {
println("用户手册") //println类型(Any?)-> Unit
}//有名函数也有类型 ()->Unit
val int2Long = fun (i: Int) = i.toLong() //匿名函数类型(Int) -> Long lambda表达式类型是Int
val sum = {
arg1: Int, arg2: Int ->
println("您输入的数字分别是 $arg1 和 $arg2")
arg1 + arg2 //最后一行 即 返回值
} //(Int,Int) -> Int
fun main(args: Array<String>) {
println(sum(2,4)) //省去invoke
println(sum.invoke(2,4))
」
//简化写法
args.forEach (::println)
//lambda 没起名
args.forEach {
if (it == "t") return //这种写法 跳出的是main函数
println(it)
}
println("The End") //不会被打印
//----------------------------------------
//如果想跳出lambda 要起个名字
args.forEach ForEach@{
if (it == "t") return@ForEach //这种写法 跳出的是main函数
println(it)
}
println("The End") //可以打印
class A {
var b = 0
get() {
println("some one get b")
return field
}
set(value) {
println("some one set b")
field = value
}
}
class A {
//var 延时初始化用 lateinit
lateinit var c: String
lateinit var d: X
//val 延时加载用 by lazy
val e: X by lazy {
println("init_X")
X()
}
}
class X
class Complex(var real: Double, var imaginary: Double) {
//操作符 加法 参数不同可重载
operator fun plus(other: Complex): Complex {
return Complex(real + other.real, imaginary + other.imaginary)
}
//参数int
operator fun plus(other: Int): Complex {
return Complex(real + other, imaginary )
}
operator fun invoke():Double {
//函数返回它的所有参数的平方和的平方根
return Math.hypot(real, imaginary)
}
//随便定义的内部方法
fun xxx(other: Complex): Complex{
//做的实际上是减法操作 可以定义正规的operator
return Complex(real - other.real, imaginary - other.imaginary)
}
//方便打印
override fun toString(): String {
return "$real , ${imaginary}i"
}
}
fun main(args: Array<String>) {
val c1 = Complex(3.0, 4.0) //3 + 4i
val c2 = Complex(2.0, 7.5) //2 + 7.5i
println(c1 + c2) //5.0 , 11.5i
println(c1 + 5) //8.0 , 4.0i
println(c1()) //5.0
println(c1.xxx(c2)) //1.0 , -3.5i
if ("_name" in args) {
//.indexOf 返回角标,如果没有返回-1
println(args[args.indexOf("_name") + 1])
}
//中缀表达式 仿in写法
if (Book() on Desk()) {//dsl
}
}
class Book {
//infix中缀表达式
infix fun on(any: Any): Boolean {
return false
}
}
class Desk
private const val USER_NAME = "tong"
private const val PASSWORD = "123"
private const val DEBUG = 0
private const val RELEASE = 1
fun main(args: Array<String>) {
//用val mode 接收if表达式
val mode = if (args[0].isNotEmpty() && args[0] == "1") {
RELEASE
} else {
DEBUG
}
println("请输入用户名:")
val username = readLine()
println("请输入密码:")
val psw = readLine()
if (mode == RELEASE && username == USER_NAME && psw == PASSWORD) {
println("Release登录成功")
} else if (username == USER_NAME && psw == PASSWORD) {
println("登录成功")
} else {
println("登录失败")
}
}
fun main(args: Array<String>) {
val x = 5
when(x) {
is Int -> println("$x is Int")
in 0..100 -> println("$x is in 0..100")
!in 0..100 -> println("$x is not in 0..100")
args[0].toInt() -> println("$x 与 args 一样")
}
//when 语句 替换 if
val mode = when{
args[0].isNotEmpty() && args[0] == "1" -> 1
else -> 0
}
}
class WhenPlayer {
internal enum class State {
PLAYING, PAUSE, BUFFERING
}
private var state = State.BUFFERING
fun pause() {
when (state) {
//不同条件做相同的事儿,可以连着写 用,分开
State.BUFFERING, State.PLAYING -> doPause()
else -> {
//相当于defualt
}
}
}
fun doPause() {
state = State.PAUSE
}
}
fun main(args: Array<String>) {
//普通循环
for (arg in args) {
println(arg)
}
//可以连角标也打印出来的循环 withIndex
for ((index, value) in args.withIndex()) {
println("$index -> $value")
}
//自定义Iterator
var list = MyIntList()
list.add(6)
list.add(7)
list.add(8)
for (i in list) {
println(i)
}
}
//自定义Iterator 先自定义集合
class MyIntList {
//初始化集合
private val list = ArrayList<Int>()
//添加add方法
fun add (int: Int) {
list.add(int)
}
//删除方法
fun remove (int: Int) {
list.remove(int)
}
//operator Iterator 方法 把自定义MyIterator return出去
operator fun iterator(): MyIterator {
return MyIterator(list.iterator())
}
}
//自定义MyIterator
class MyIterator(val iterator: Iterator<Int>) {
//operator next()方法
operator fun next(): Int {
return iterator.next()
}
//和 hasNext方法
operator fun hasNext(): Boolean {
return iterator.hasNext()
}
}
Outter@ for(i in list) {
Inner@ while (i > 0 ) {
if (i == 7) {
break@Outter// 跳出外层for循环
// break //跳出while循环
}
}
}
fun main(vararg args: String) {//vararg 可变参数
//等于 fun main(args: Array) {
for (arg in args) {
println(arg)
}
}
//可变参数 可以放在任意位置
fun hello(vararg ints: Int, str: String) {
ints.forEach(::println)
println(str)
}
fun main(args: Array<String>) {
hello(1,2,3,4,str = "hello") //具名参数 str =
//也可以传入array
val array = intArrayOf(1,2,3,4)
hello(*array, str = "hello")// * 可以展开数组
//* 不支持list
val list = arrayListOf(1,2,3,4)
hello(~~*list~~ , str = "hello")
}
fun defualt(int: Int = 0, str: String) {
print("$int , $str")
}
defualt(str = "hello")