<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                [TOC] ## **頂層聲明的可見性** 如果頂層聲明是 private 的,它是聲明它的文件所私有的(參見 可見性修飾符)。 ### **[可見性修飾符](http://www.kotlincn.net/docs/reference/visibility-modifiers.html)** 類、對象、接口、構造函數、方法、屬性和它們的 setter 都可以有可見性修飾符(getter總是與屬性有著相同的可見性)。 在 Kotlin 中有這**四個可見性修飾符:private、protected、internal 和 public** 。 如果沒有顯式指定修飾符的話,默認可見性是public 。 下面將根據聲明作用域的不同來解釋。 **包名** 函數、屬性和類、對象和接口可以在頂層聲明,即直接在包內: ~~~ // 文件名:example.kt package foo fun baz() {} class Bar {} ~~~ * 如果你**不指定任何可見性修飾符,默認為 public**,這意味著你的聲明將隨處可見; * 如果你聲明為 **private** ,它**只會在聲明它的文件內可見**; * 如果你聲明為 **internal** ,它**會在相同模塊內隨處可見**; * **protected 不適用于頂層聲明**。 比如: ~~~ // 文件名:example.kt package foo private fun foo() {} // 在 example.kt 內可見 public var bar: Int = 5 // 該屬性隨處可見 private set // setter 只在 example.kt 內可見 internal val baz = 6 // 相同模塊內可見 ~~~ **類和接口** 對于類內部聲明的成員: * private 意味著只在這個類內部(包含其所有成員)可見; * protected —— 和 private 一樣 + 在子類中可見。 * internal —— 能見到類聲明的 本模塊內 的任何客戶端都可見其 internal 成員; * public —— 能見到類聲明的任何客戶端都可見其 public 成員。 >[warning] **注意** :對于Java用戶:Kotlin 中外部類不能訪問內部類的 private 成員。 如果你覆蓋一個 protected 成員并且沒有顯式指定其可見性,該成員還會是 protected 可見性。 例子: ~~~ open class Outer { private val a = 1 protected open val b = 2 internal val c = 3 val d = 4 // 默認 public protected class Nested { public val e: Int = 5 } } class Subclass : Outer() { // a 不可見 // b、c、d 可見 // Nested 和 e 可見 override val b = 5 // “b”為 protected } class Unrelated(o: Outer) { // o.a、o.b 不可見 // o.c 和 o.d 可見(相同模塊) // Outer.Nested 不可見,Nested::e 也不可見 } ~~~ **構造函數** 要指定一個類的的主構造函數的可見性,使用以下語法(注意你**需要添加一個顯式constructor 關鍵字**): ~~~ class C private constructor(a: Int) { …… } ~~~ 這里的構造函數是私有的。**默認情況下,所有構造函數都是 public** ,這實際上等于類可見的地方它就可見(即 一個 internal 類的構造函數只能 在相同模塊內可見). **局部聲明** 局部變量、函數和類不能有可見性修飾符。 **模塊** 可見性修飾符 internal 意味著該成員只在相同模塊內可見。更具體地說, 一個模塊是編譯在一起的一套 Kotlin 文件: * 一個 IntelliJ IDEA 模塊; * 一個 Maven 或者 Gradle 項目; * 一次 <kotlinc> Ant 任務執行所編譯的一套文件。 ### 定義函數 Kotlin中函數的結構是怎樣的呢? ![](http://upload-images.jianshu.io/upload_images/7368752-7c934d7214fe0662.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) * **帶有兩個 Int 參數、返回 Int 的函數**: ~~~ fun sum(a: Int, b: Int): Int { return a + b } fun main(args: Array<String>) { print("sum of 3 and 5 is ") println(sum(3, 5)) } ~~~ * **將表達式作為函數體、返回值類型自動推斷的函數**: ~~~ fun sum(a: Int, b: Int) = a + b fun main(args: Array<String>) { println("sum of 19 and 23 is ${sum(19, 23)}") } ~~~ 完整代碼如下 ~~~ fun main(args: Array<String>) { println("sum of 19 and 23 is ${sum(19, 23)}") } fun sum(a: Int, b: Int) = a + b //Kotlin可以根據函數里的參數類型推測函數的返回類型 ~~~ 輸出結果: ~~~ sum of 19 and 23 is 42 ~~~ 從輸出結果可以看出,**Kotlin還支持類似JavaScript的 $占位符操作**。 >[info] $是一種javascript定義的符號,比如說$(document),可以獲得當前頁面的上下文,就是一個變量名而已。 > 如果在jquery框架里面的話它代表jquery本身。 > 其它時候它只是一個變量名,僅此而已。代表著一個事物,可以代表函數,參數等 > 比如 ``` var $ = function(id) { return document.getElementById(id); }; ``` 那么現在$就代表一個函數了,直接`$('myDiv')`,就等同于`document.getElementById('myDiv')`; * **函數返回無意義的值**: ~~~ fun printSum(a: Int, b: Int): Unit { println("sum of $a and $b is ${a + b}") } fun main(args: Array<String>) { printSum(-1, 8) } ~~~ 其中的 ~~~ fun printSum(a: Int, b: Int): Unit { println("sum of $a and $b is ${a + b}") } ~~~ 相當于Java的 ~~~ public void printSum(int a, int b){ println("sum of "+ a + "and"+ b +"is"+ (a + b)); } ~~~ 其實可以把Unit省略掉 * **Unit 返回類型可以省略**: ~~~ fun printSum(a: Int, b: Int) { println("sum of $a and $b is ${a + b}") } fun main(args: Array<String>) { printSum(-1, 8) } ~~~ >[info] **注意**:Java的void是沒有返回值的,而Kotlin的Unit卻有返回值,只是返回值 no meaningful(無意義)。 ### **Kotlin號稱的nullpointer檢測** 其實是這樣實現的: ~~~ fun parseInt(str: String): Int? { // ...省略String to Int的轉換代碼 //Return null if str does not hold an integer //當str不是integer類型的時候,函數將返回null } ~~~ 測試代碼 ~~~ fun parseInt(str: String): Int? {//返回類型 Int后面加上一個 ? 就表示函數有可能是返回null return str.toIntOrNull() //這是Kotlin的Api } fun printProduct(arg1: String, arg2: String) { val x = parseInt(arg1) val y = parseInt(arg2) // Using `x * y` yields error because they may hold nulls. if (x != null && y != null) { // x and y are automatically cast to non-nullable after nullcheck println(x * y) } else { println("either '$arg1' or '$arg2' is not a number") } } fun main(args: Array<String>) { printProduct("6", "7") printProduct("a", "7") printProduct("a", "b") } ~~~ 運行結果 ~~~ 42 either 'a' or '7' is not a number either 'a' or 'b' is not a number ~~~ 從上面的代碼,`str.toIntOrNull()`,我們點擊,查看源碼,得知`“Parses the string as an [Int] number and returns the result or `null` if the string is not a valid representation of a number.”`直譯就是“將字符串解析為 [Int] 數字,如果該字符串不是數字的有效表示形式,則返回結果或 "null"。” ### **Kotlin的變量類型檢查**: 代碼如下 ~~~ fun getStringLength(obj: Any): Int? { if (obj is String) { //is相當于java里的instance of // `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 } fun main(args: Array<String>) { fun printLength(obj: Any) { println("'$obj' string length is ${getStringLength(obj) ?: "... err, not a string"} ") } printLength("Incomprehensibilities") printLength(1000) } ~~~ 運行結果 ~~~ 'Incomprehensibilities' string length is 21 '1000' string length is ... err, not a string ~~~ 這里有必要講解一下“`?:`”這個符號, * **?.(安全調用符)** 先看一下這個 **?.(安全調用符)**,安全調用符的出現為了解決什么問題?可空類型變量不能直接使用,但是直接使用非空判斷又過于復雜,所以可以使用安全調用符。 怎么使用安全調用符?之前的結構是【變量.方法】,現在的結構是【**變量?.方法**】。 使用了安全調用符,代碼執行邏輯是怎樣的?變量不會NULL的時候,才去執行方法,所以不會報空指針。變量為NULL的時候,【變量?.方法】的結果為NULL。 ![](http://upload-images.jianshu.io/upload_images/7368752-52651a80c6cb6723.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) * **?:( Elvis操作符)** 針對【變量?.方法】,如果變量為NULL,【變量?.方法】的返回結果是NULL,那我們能不能**指定想返回的值**呢?答案是肯定的,我們需要配合Elvis操作符,使用方式為【變量?:值 】 ![](http://upload-images.jianshu.io/upload_images/7368752-338762cd48b16b00.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看