Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Person (val name: String, val age: Int) {
- fun greeting() = println("嗨!年紀 $age 歲的 $name!")
- fun greeting2() = println("YOYO!年紀 $age 歲的 $name!")
- }
- fun operateWithSonic(f: Person.() -> Unit) =
- f(Person("Sonic", 30))
- // 節錄自 Kotlin 官方教學程式碼
- fun happyBirthday(name: String, age: Int): String {
- return "Happy ${age}th birthday, $name!"
- }
- // 如果函式只有一行 return 的話,可簡寫成等式
- fun happyBirthday2(name: String, age: Int) =
- "Happy ${age}th birthday, $name!"
- // 無回傳參數的型態為 Unit 或不寫亦可
- fun happyBirthday3(name: String, age: Int): Unit =
- println("Happy ${age}th birthday, $name!")
- val greeting = happyBirthday("Anne", 32)
- // try-catch
- fun divideOrZero(numerator: Int, denominator: Int): Int {
- try {
- return numerator / denominator
- } catch (e: ArithmeticException) {
- println("error: x/0")
- return 0
- }
- }
- fun main() {
- // declare variables - val (read-only) var (writable)
- val a_1: Int = 2
- var a_2: Double = 2.0
- var a_3: Boolean = true
- var a_4: String = "heyMan"
- // String Interpolation
- var b_1: Int = 10
- var b_2: Int = 20
- var b_3: String = "The number ($b_1 + $b_2 + 100) = ${b_1 + b_2 + 100}"
- println(b_3)
- // Condition Flow: if
- val c_1: Int = 10
- if (c_1 > 10) {
- println("big number")
- }else {
- println("small number")
- }
- // Condition Flow: if-expression
- var c_result = if (c_1 > 10) {
- "big number"
- }else {
- "small number"
- }
- println("c_result: " + c_result)
- // Condition Flow: when (like switch in JAVA)
- when (c_1) {
- in 0..9 -> println("big number")
- else -> println("small number")
- }
- // Condition Flow: when-expression
- var c_result_2 = when (c_1) {
- in 0..9 -> "big number"
- else -> "small number"
- }
- println("c_result_2: " + c_result_2)
- // Collection: array (like int[] in JAVA)
- val integers = intArrayOf(1, 2, 3)
- val doubles = doubleArrayOf(1.0, 2.0, 3.0)
- // Collection: array (like Integer[] in JAVA)
- val generalIntegers = arrayOf(1, 2, 3)
- val generalDoubles = arrayOf(1.0, 2.0, 3.0)
- // Collection: List, Map, Set (Read-only)
- val strings = listOf("A","B","C")
- val map = mapOf("A" to 1, "B" to 2, "C" to 3);
- val set = setOf("a", "b", "c")
- // Collection: List, Map, Set (Writable)
- val strings_w = mutableListOf("A","B","C")
- val map_w = mutableMapOf("A" to 1, "B" to 2, "C" to 3);
- val set_w = mutableSetOf("a", "b", "c")
- // for loop
- for (word in strings) {
- print(word + " , ")
- }
- println()
- // while loop
- var d_1: Int = 3
- while (d_1 > 0) {
- println("d_1 is still positive number: $d_1")
- d_1--;
- }
- // special continue, break to jump out of outer-outer-outer loop !
- outer@ for (n in 2..100) {
- for (d in 2 until n) {
- if (n % d == 0) break@outer
- }
- println("$n is prime")
- }
- operateWithSonic { // 參數只有 Function Type 的話,連括弧都可不寫。
- greeting2()
- println("$name 真的很棒。")
- }
- divideOrZero(10,0);
- // TODO: null ? !!
- // TODO: Lambda function
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement