Kotlin 入门笔记
fun main(args: Array<String>) {
showMessage(null);
showMessage("Today is Friday")
showName("oyoung", 26)
showName(name = "Xiaohong", age = 6)
showName(age = 10, name = "Xiaogou")
println("double of 1000 is ${doubleOf(1000)}")
println(total(2.5, 6, 0.85))
println( '4' repeat 5)
println(4 * "123")
multiPrint("a", "b", "c", "d")
ValOrVar()
classUsage()
useWhen()
useLoops()
useRange()
useEquals()
useConditionExpression()
}
fun showMessage(message: String?): Unit {
message?.let {
println(it)
}
}
fun showName(name: String, age: Int = 2) : Unit {
println("My name is $name, my age is $age years old")
}
fun doubleOf(number: Int): Int {
return number + number;
}
fun total(single: Double, count: Int, percent: Double) : Double = single * count * percent
infix fun Char.repeat(times: Int) = "${this}".repeat(times)
operator fun Int.times(message: String) = message.repeat(this)
fun multiPrint(vararg messages: String) {
for (i in messages) {
print(i)
print(if (i === messages.last()) "" else ", ")
}
}
fun ValOrVar() {
val n: Int = 100
val name = "God"
var number: String? = null
var age: Int = 0
var message = "Hahahaha"
var myName = "oyoung"
var yourName: String? = "Big SB"
yourName = null
}
class One
class Two(val value: Int, var name: String)
fun classUsage() {
val one = One()
var two = Two(0, "O")
println(two.value)
two.name = "Fuck"
}
class Queue<E>(vararg items: E) {
private val children = items.toMutableList()
fun enqueue(item: E) = children.add(item)
fun dequeue(): E = children.removeAt(0)
fun size() = children.size
fun isEmpty() = children.isEmpty()
}
fun <E> queueOf(vararg items: E) : Queue<E> = Queue(*items)
fun queueUsage() {
val qa = Queue(1, 2, 3, 4)
val qb = queueOf("a", "b", "c")
qa.enqueue(8)
qb.dequeue()
}
open class Animal {
open fun say() {
println("Nothing")
}
}
open class Dog(val name: String, var age: Int): Animal() {
override fun say() {
println("Wow Wow, My name is $name, I'm $age years old")
}
}
class Keji: Dog("Keji", 0)
fun useWhen() {
for (i in 0..9) println( when(i) {
2 -> "i is 2"
is Int -> "i is int"
else -> "i is not 2"
})
}
fun useLoops() {
println("for 循环")
for (i in 0..9) {
print(i)
print(if (i == 9) "" else ", ")
}
println()
println("while 循环")
var i = 0
while (i < 10) {
print(i++)
print(if (i == 10) "" else ", ")
}
println()
println("do {} while 循环")
do {
print(i--)
print(if (i == 0) "" else ", ")
} while (i > 0)
println()
println("迭代器循环")
val list = listOf(0, 1, 2, 3, 4)
val it = list.iterator()
while (it.hasNext()) {
print(it.next())
print(if (it.hasNext() ) ", " else "")
}
println()
}
fun useRange() {
val range1 = 0..5
val range2 = 0..6 step 2
val range3 = 5 downTo 0
val range4 = 50 downTo 0 step 10
println("Range: ")
range1.forEach {
print(it)
print(if (it == range1.last) "" else ", ")
}
println()
range2.forEach {
print(it)
print(if (it == range2.last) "" else ", ")
}
println()
range3.forEach {
print(it)
print(if (it == range3.last) "" else ", ")
}
println()
range4.forEach {
print(it)
print(if (it == range4.last) "" else ", ")
}
println()
}
fun useEquals() {
val a = setOf(1, 2, 3)
val b = setOf(3, 2, 1)
println(a == b)
println(a === b)
}
fun useConditionExpression() {
var number = 9
if (number % 2 == 0) {
println("偶数")
} else {
println("奇数")
}
println( if(number % 3 == 0) "是3的倍数" else "不是3的倍数" )
}