fun main(args: Array<String>) {
val arrays = (0..10)
var arrays1 = (2..8)
/**
* 获得基数
*/
arrays.filter { it % 2 == 1 }.forEach(::println)
/**
* takeWhile只获取满足条件之前的数据
*/
arrays1.takeWhile { it % 2 == 0 }.forEach(::println)
}
@Pojos
data class WorlderCup(val name: String, val old: Int, val host: String) {
fun getTeamCount() {
println("${name}参赛队伍一共32支")
}
}
fun main(args: Array<String>) {
/**
* 使用let正对于某个对象进行操作
* 返回=》WorlderCup(name=俄罗斯世界杯, old=84, host=俄罗斯)
*/
worldercup().let {
println(it)
}
//返回=>10000
100.let { println(it * 100) }
/**
* apply相当于this
* 返回=>俄罗斯世界杯参赛队伍一共32支
*/
worldercup().apply {
getTeamCount()
}
/**
* with把对象和this合并到一起
*/
with(worldercup(), {
println(name)
})
/**
* 使用with函数返回=》阿根廷不要为我哭泣
*/
var bufferread = BufferedReader(FileReader("worldcup.txt"))
with(bufferread) {
var line: String
while (true) {
line = readLine() ?: break
print(line)
}
close()
}
/**
* 读取BuffererReader
*/
// println(bufferread.readText())
/**
* 返回的对象有closeable方法的话 调用use 就不用手动调用close方法
*/
BufferedReader(FileReader("worldcup.txt")).use {
var line: String
while (true) {
line = it.readLine() ?: break
print(line)
}
}
}
fun worldercup(): WorlderCup {
return WorlderCup("俄罗斯世界杯", 84, "俄罗斯")
}