main.kt
import utils.Person1
import utils.serialize
import java.util.*
fun main(args: Array<String>){
println("hello,world")
val 课程 = Triple(3,"学会","Kotlin")
val 费用 = Pair("学费",0)
print("${课程.first}天${课程.second}${课程.third}")
println(",${费用.first}${费用.second}元!")
val 一到一百 = 1..100
println("正序一到一百:")
for(j in 一到一百)
print(" ${j}")
println()
println("倒序10到0:")
val 倒计时 = 10.downTo(0)
for (i in 倒计时)
print("${i} ")
println("\n 一到一百奇数:")
val 一到一百奇数 = 一到一百.step(2)
for (i in 一到一百奇数)
print("${i} ")
println("\n 翻转区间:")
for (i in 一到一百奇数.reversed())
print("${i} ")
println()
val s = " "
println("s的长度是${s.isNotEmpty()}")
var array = arrayOf("嘉定北","徐家汇","南翔","桃浦新村","徐家汇","上海西站","罗山路","迪士尼","嘉定新城","徐家汇","桃浦新村")
println("array数组的长度是" + array.count() +"/${array.size}")
println("第一个元素${array.first()}/${array.component1()}")
println("第二个元素${array.component2()} ..其他同理 component3.4.5 但是只到五")
println("最后一个元素${array.last()}")
println("检查嘉定北是否在数组中,结果是嘉定北${if("嘉定北" in array)"在" else "不在"}数组中")
println("查找含有‘迪’字的第一个元素,结果是${array.find { it.contains("迪") }}")
println("同理,查找最后一个用findlast,查找含有‘徐’的最后一个元素,结果是${array.findLast { it.contains("徐") }}")
println("将数组array输出为字符串:${array.joinToString()}")
println("将数组输出为字符串花括号版:${array.joinToString { it + "站" }}")
println("圆括号加强版:${array.joinToString("-","地铁线路","",4)}")
var mutamap = mutableMapOf<String,Any>(Pair("姓名", listOf(mapOf("1" to "张三", "2" to "李四"))), Pair("年龄",20), Pair("性别","男"))
println(mutamap)
var lists = listOf<String>("鸡蛋","大米","冰激凌")
println(lists[0])
for((i,e) in lists.withIndex())
println("$i $e")
var map =TreeMap<String,Any>()
map["好"] = "good"
map["学习"] = "study"
println(map)
data class County(val name: String, val peopleCount: Int)
data class City(val name: String, val counties: List<County>)
data class Province(val name: String, val size: Int, val cities: List<City>?)
val c1 = County("湘潭县", 20_000)
val c2 = County("株洲县", 30_000)
val c4 = County("攸县", 40_000)
val ci1 = City("湘潭市", listOf(c1))
val ci2 = City("株洲市", listOf(c2, c4))
val p1 = Province("湖南省", 780, listOf(ci1, ci2))
println(serialize(p1))
data class Person(val name: String, val age: Int, val likes: List<String>)
val p = Person("better", 30, listOf("Java", "Kotlin", "Python"))
println(serialize(p))
}
util.kt //摘抄某位大神的手笔,忘了地址,如若侵犯权益,请联系本人删除 谢谢
package utils
import interFace.Apple
import java.lang.StringBuilder
import kotlin.reflect.KProperty1
import kotlin.reflect.full.memberProperties
fun serialize(obj: Any) = buildString { serializeObj(obj) }
private inline fun StringBuilder.serializeObj(o: Any) {
o.javaClass.kotlin.memberProperties.joinToStringBuilder(this, separator = ",", prefix = "{", postfix = "}") {
serializeProperty(it, o)
}
}
private inline fun StringBuilder.serializeProperty(property: KProperty1<Any, *>, receiver: Any) {
val key = property.name
serializeString(key)
append(": ")
val value = property.get(receiver)
serializePropertyValue(value)
}
private fun StringBuilder.serializePropertyValue(value: Any?) {
when (value) {
null -> append("null")
is String -> serializeString(value)
is Boolean, is Number -> append(value.toString())
is List<*> -> serializeList(value)
else -> serializeObj(value)
}
}
private fun StringBuilder.serializeList(data: List<Any?>) {
data.joinToStringBuilder(this, separator = ", " ,prefix = "[", postfix = "]") {
serializePropertyValue(it)
}
}
private inline fun StringBuilder.serializeString(name: String) {
append('\"').append("$name").append('\"')
}
fun <T> Iterable<T>.joinToStringBuilder(sb: StringBuilder, separator: CharSequence = ", ",
prefix: CharSequence = "",
postfix: CharSequence = "",
limit: Int = -1,
truncated: CharSequence = "...",
transform: ((T) -> Unit)? = null): StringBuilder {
return joinTo(sb, separator, prefix, postfix, limit, truncated) {
if (transform == null) {
return@joinTo it.toString()
}
transform.invoke(it)
""
}
}
class Person1(age: Int): Apple{
var age = age
val isAdult: Boolean
get() = age >= 18
override fun print() = println("this is person1 class")
}
interface.kt
package interFace
interface Apple{
fun print(){
println("hello,world")
}
}