<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之旅 廣告
                # Kotlin 對象聲明和表達式 > 原文: [https://www.programiz.com/kotlin-programming/object-singleton](https://www.programiz.com/kotlin-programming/object-singleton) #### 在本文中,您將在示例的幫助下了解對象聲明(單例)和對象表達式。 ## 對象聲明 單例是一種面向對象的模式,其中一個類只能有一個實例(對象)。 例如,您正在處理具有 SQL 數據庫后端的應用。 您想創建一個連接池來訪問數據庫,同時為所有客戶端重用相同的連接。 為此,您可以通過單例類創建連接,以便每個客戶端獲得相同的連接。 * * * Kotlin 提供了一種使用對象聲明功能創建單例的簡單方法。 為此,使用了`object`關鍵字。 ```kt object SingletonExample { ... .. ... // body of class ... .. ... } ``` 上面的代碼結合了一個類聲明和該類的單個實例`SingletonExample`的聲明。 對象聲明可以包含屬性,方法等。 但是,不允許它們具有構造器(這很有意義)。 **為什么?** 與普通類的對象類似,可以使用`.`表示法調用方法和訪問屬性。 * * * ### 示例:對象聲明 ```kt object Test { private var a: Int = 0 var b: Int = 1 fun makeMe12(): Int { a = 12 return a } } fun main(args: Array<String>) { val result: Int result = Test.makeMe12() println("b = ${Test.b}") println("result = $result") } ``` 運行該程序時,輸出為: ```kt b = 1 result = 12 ``` * * * 對象聲明可以像普通類一樣從類和接口繼承。 * * * ### 單例和依賴注入 對象聲明有時會很有用。 但是,它們在與系統許多其他部分交互的大型軟件系統中并不理想。 **推薦閱讀**: [依賴注入&單例設計模式](https://stackoverflow.com/questions/2662842/dependency-injection-singleton-design-pattern) * * * ## Kotlin 對象表達式 `object`關鍵字也可以用于創建稱為匿名對象的匿名類的對象。 如果您需要創建一個對某些類或接口稍加修改而又不為其聲明子類的對象,則可以使用它們。 例如 , ```kt window.addMouseListener(object : MouseAdapter() { override fun mouseClicked(e: MouseEvent) { // ... } override fun mouseEntered(e: MouseEvent) { // ... } }) ``` (該示例摘自 [Kotlin 官方文檔頁面](https://kotlinlang.org/docs/reference/object-declarations.html)。) 在此,聲明了擴展`MouseAdapter`類的匿名對象。該程序覆蓋了`mouseClicked()`和`mouseEntered()`這兩個`MouseAdapter`方法。 如有必要,可以為匿名對象分配名稱,并將其存儲在變量中。 例如, ```kt val obj = object : MouseAdapter() { override fun mouseClicked(e: MouseEvent) { // ... } override fun mouseEntered(e: MouseEvent) { // ... } } ``` * * * ### 示例:Kotlin 對象表達式 ```kt open class Person() { fun eat() = println("Eating food.") fun talk() = println("Talking with people.") open fun pray() = println("Praying god.") } fun main(args: Array<String>) { val atheist = object : Person() { override fun pray() = println("I don't pray. I am an atheist.") } atheist.eat() atheist.talk() atheist.pray() } ``` 運行該程序時,輸出為: ```kt Eating food. Talking with people. I don't pray. I am an atheist. ``` 在這里,匿名對象存儲在變量`atheist`中,該變量通過`pray()`方法實現了`Person`類。 * * * 如果要實現的類具有用于聲明匿名對象的構造器,則需要傳遞適當的構造器參數。 例如, ```kt open class Person(name: String, age: Int) { init { println("name: $name, age: $age") } fun eat() = println("Eating food.") fun talk() = println("Talking with people.") open fun pray() = println("Praying god.") } fun main(args: Array<String>) { val atheist = object : Person("Jack", 29) { override fun pray() = println("I don't pray. I am an atheist.") } atheist.eat() atheist.talk() atheist.pray() } ``` 運行該程序時,輸出為: ```kt name: Jack, age: 29 Eating food. Talking with people. I don't pray. I am an atheist. ```
                  <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>

                              哎呀哎呀视频在线观看