Advertisement
uopspop

Untitled

Sep 2nd, 2020
1,474
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.34 KB | None | 0 0
  1. class Person (val name: String, val age: Int) {
  2.     fun greeting() = println("嗨!年紀 $age 歲的 $name!")
  3.     fun greeting2() = println("YOYO!年紀 $age 歲的 $name!")
  4. }
  5.  
  6. fun operateWithSonic(f: Person.() -> Unit) =
  7.     f(Person("Sonic", 30))
  8.  
  9. // 節錄自 Kotlin 官方教學程式碼
  10. fun happyBirthday(name: String, age: Int): String {
  11.     return "Happy ${age}th birthday, $name!"
  12. }
  13.  
  14. // 如果函式只有一行 return 的話,可簡寫成等式
  15. fun happyBirthday2(name: String, age: Int) =
  16.     "Happy ${age}th birthday, $name!"
  17.  
  18. // 無回傳參數的型態為 Unit 或不寫亦可
  19. fun happyBirthday3(name: String, age: Int): Unit =
  20.     println("Happy ${age}th birthday, $name!")
  21.  
  22. val greeting = happyBirthday("Anne", 32)
  23.  
  24. // try-catch
  25. fun divideOrZero(numerator: Int, denominator: Int): Int {
  26.     try {
  27.         return numerator / denominator
  28.     } catch (e: ArithmeticException) {
  29.         println("error: x/0")
  30.         return 0
  31.     }
  32. }
  33.  
  34. fun main() {
  35.  
  36.     // declare variables - val (read-only) var (writable)
  37.     val a_1: Int = 2
  38.     var a_2: Double = 2.0
  39.     var a_3: Boolean = true
  40.     var a_4: String = "heyMan"
  41.  
  42.     // String Interpolation
  43.     var b_1: Int = 10
  44.     var b_2: Int = 20
  45.     var b_3: String = "The number ($b_1 + $b_2 + 100) = ${b_1 + b_2 + 100}"
  46.     println(b_3)
  47.  
  48.     // Condition Flow: if
  49.     val c_1: Int = 10
  50.     if (c_1 > 10) {
  51.         println("big number")
  52.     }else {
  53.         println("small number")
  54.     }
  55.     // Condition Flow: if-expression
  56.     var c_result = if (c_1 > 10) {
  57.         "big number"
  58.     }else {
  59.         "small number"
  60.     }
  61.     println("c_result: " + c_result)
  62.  
  63.     // Condition Flow: when (like switch in JAVA)
  64.     when (c_1) {
  65.         in 0..9 -> println("big number")
  66.         else -> println("small number")
  67.     }
  68.     // Condition Flow: when-expression
  69.     var c_result_2 = when (c_1) {
  70.         in 0..9 -> "big number"
  71.         else -> "small number"
  72.     }
  73.     println("c_result_2: " + c_result_2)
  74.  
  75.     // Collection: array (like int[] in JAVA)
  76.     val integers = intArrayOf(1, 2, 3)
  77.     val doubles = doubleArrayOf(1.0, 2.0, 3.0)
  78.  
  79.     // Collection: array (like Integer[] in JAVA)
  80.     val generalIntegers = arrayOf(1, 2, 3)
  81.     val generalDoubles = arrayOf(1.0, 2.0, 3.0)
  82.  
  83.     // Collection: List, Map, Set (Read-only)
  84.     val strings = listOf("A","B","C")
  85.     val map = mapOf("A" to 1, "B" to 2, "C" to 3);
  86.     val set = setOf("a", "b", "c")
  87.  
  88.     // Collection: List, Map, Set (Writable)
  89.     val strings_w = mutableListOf("A","B","C")
  90.     val map_w = mutableMapOf("A" to 1, "B" to 2, "C" to 3);
  91.     val set_w = mutableSetOf("a", "b", "c")
  92.  
  93.     // for loop
  94.     for (word in strings) {
  95.         print(word + " , ")
  96.     }
  97.     println()
  98.  
  99.     // while loop
  100.     var d_1: Int = 3
  101.     while (d_1 > 0) {
  102.         println("d_1 is still positive number: $d_1")
  103.         d_1--;
  104.     }
  105.  
  106.     // special continue, break to jump out of outer-outer-outer loop !
  107.     outer@ for (n in 2..100) {
  108.         for (d in 2 until n) {
  109.             if (n % d == 0) break@outer
  110.         }
  111.         println("$n is prime")
  112.     }
  113.  
  114.     operateWithSonic {  // 參數只有 Function Type 的話,連括弧都可不寫。
  115.         greeting2()
  116.         println("$name 真的很棒。")
  117.     }
  118.  
  119.     divideOrZero(10,0);
  120.  
  121.  
  122.     // TODO: null ? !!
  123.     // TODO: Lambda function
  124.    
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement