基本語法示例
實例代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
package com.stone.basic.syntax /** * desc : * author: stone * email : [email protected] * time : 27/05/2017 11 01 */ class BasicSyntax { //Function having two Int parameters with Int return type: public fun sum(a: Int, b: Int): Int { //訪問修飾符 省略時,默認為 public return a + b } //Function having three Int parameters with Int return type: fun sum(a: Int, b: Int, c: Int) = a + b + c //Function returning no meaningful value: fun printSum(a: Int, b: Int): Unit { //Unit為無類型,類似java中的void,可以省略 println( "sum of " + a + " and " + b + " is ${a + b}" ) println( "sum of $a and $b is ${a + b}" ) //在雙引號中 直接用 $符操作變量 與上句等價 } fun assignVarible() { val a: Int = 1 // immediate assignment val = 本地只讀變量 即不可變 immutable val b = 2 // `Int` type is inferred 自動類型推斷 val c: Int // Type required when no initializer is provided c = 3 // deferred assignment var x = 1 // Mutable variable: x++ val s1 = "x is $x" // simple name in template: val s2 = "${s1.replace(" is ", " was ")}, but now is $x" // arbitrary expression in template: println(s2) } fun maxOf(a: Int, b: Int): Int { // return a > b ? a : b; //原java中的三目運算符 不可用 if (a > b) return a else return b } //fun maxOf(a:Int, b: Int):Int fun minOf(a: Int, b: Int): Int = if (a < b) a else b //字符串轉int private fun parseInt(str: String): Int? { // ? 表示可以為空 return str.toIntOrNull( 8 ) //參數為 進制數(radix), 不傳默認為10 轉換錯誤 返回null } fun getBaseSyntax(name: String?): BasicSyntax? { // ? 表示可以為空 // checkNotNull(name) // 參數不能為空的 檢測函數 return BasicSyntax() } fun printProduct(arg1: String, arg2: String) { val x1 = parseInt(arg1) val x2 = parseInt(arg2) if (x1 == null ) return if (x2 == null ) return println(x1 * x2) } //is operator if (obj is String) { // 類似java中的 instanceof // `obj` is automatically cast to `String` in this branch return obj.length } // `obj` is still of type `Any` outside of the type-checked branch return null } // !is fun getStringLength2(obj: Any): Int? { if (obj !is String) return null return obj.length } fun getStringLength3(obj: Any): Int? { if (obj is String && obj.length > 0 ) return obj.length return null } //Using a for loop fun foreachItems() { // val items = listOf<String>("apple", "banana", "kiwi") val items = listOf( "apple" , "banana" , "kiwi" ) for (item in items) { //in operator println( "item is $item" ) } for (index in items.indices) { //indices 索引 type: Collection // println("item at $index is ${items.get(index)}") println( "item at $index is ${items[index]}" ) //使用[index] 而不用 .get(index) } } //Using when expression fun describe(obj: Any): String = when (obj) { //when 中 必須 有一個else 1 -> "One" "Hello" -> "Greeting" is Long -> "Long" !is String -> "not a string" else -> "Unknown" } //Using ranges 如果在if中 check的是一個數值,且使用了 in operator fun range() { val x = 10 ; val y = 9 //同一行中使用 ; 來分隔 if (x in 1 ..y + 1 ) { //使用 .. 來表示范圍 最后轉換成 x in 1..10 // if (x in (1..(y + 1))) {//如此解釋 執行順序 沒問題 最后轉換成 x in 1..10 // if (x in ((1..y) + 1)) {如此解釋 執行順序 不行 最后轉換成 x in 10 println( "fits in range" ) } for (x in 1 .. 5 ) { //include 5 } for (x in 1 .. 10 step 2 ) { //x+=2 x is in {1, 3, 5, 7, 9} println( "rang 1..10 step 2: $x" ) } for (x in 9 downTo 0 step 3 ) { //x=9, x>=0 x-=3 println( "x in 9 downTo 0 step 3: $x" ) } for (x in 0 until 10 step 2 ) { //until 10 : not include 10 println( "x in 1 until 10: $x" ) } } //Checking if a collection contains an object using in operator: fun contains() { val list = listOf( "a1" , "a2" , "a3" ) //不可變list when { // 匹配到一個條件 其它 就不再匹配 "a4" in list -> println( "壹" ) "a5" in list -> println(list.size) "a3" in list -> println( "the index is ${list.indexOf(" a3 ")}" ) } } //Using lambda expressions to filter and map collections: fun collectionsLambda() { // val list = mutableListOf<Int>() //可變list // for (i in 1 ..10) { // list.add(i) // // } val list = ( 1 .. 10 ).toList() //上面的 簡寫 list.filter { it % 2 == 0 }.map { it * 3 }.forEach(::println) // list.filter { it % 2 == 0 }.map { it * 3 }.forEach{ println("item is $it")} } } fun main(args: Array<String>) { var base = BasicSyntax() base.printSum( 10 , 20 ) base.assignVarible() var min = base.minOf( 10 , 20 ) println( "min number is $min" ) base.getBaseSyntax( null ) base.printProduct( "1" , "kk" ) base.printProduct( "33" , "66" ) println( null ) //直接輸出了 null 字符串 base.foreachItems() println(base.describe( 2 )) base.range() base.contains() base.collectionsLambda() } |
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
原文鏈接:http://blog.csdn.net/jjwwmlp456/article/details/72780787