<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                抬頭 --- * 屬性相關 * var、val * 默認實現的get、set * 幕后字段 * 常量放在那里 * lateinit * 委托 * 方法相關 * 對象相關 * **object** --- [TOC] ### object聲明(object declarations) #### **使用object聲明方便地實現單例模式** ~~~ object DataProviderManager { fun registerDataProvider(provider: DataProvider) { // ... } val allDataProviders: Collection<DataProvider> get() = // ... } ~~~ 以上就是一個對象聲明,和聲明一個變量一樣。使用如下: ~~~ DataProviderManager.registerDataProvider(...) ~~~ 如此就完成了一個方便的單例模式的構造。 另外object是可以有父類的: ~~~ object DefaultListener : MouseAdapter() { override fun mouseClicked(e: MouseEvent) { // ... } override fun mouseEntered(e: MouseEvent) { // ... } } ~~~ 注意:object不可以作為內部類使用 #### companion object 在類的內部使用object,它的性質就成了這個類的靜態部分,需要使用companion object ~~~ class MyClass { companion object Factory { fun create(): MyClass = MyClass() } } ~~~ 使用如下: ~~~ val instance = MyClass.create() ~~~ 很明顯,這就是一個靜態方法的調用。(Kotlin中沒有類似java里買呢static關鍵字的用法) #### 靜態方法有了,那么靜態屬性怎么實現? companion object的名字是可以省略的,這時候直接把需要的靜態屬性和靜態方法都放進去就OK了。 值得注意的是,靜態內部類(就是直接在一個類里面用class聲明的類)是不可以訪問它的外部類的普通成員的,那么怎么辦?訪問其外部類的companion object的成員就OK了。 舉例說明: ~~~ class NotificationModule(context: ReactApplicationContext?) : ReactContextBaseJavaModule(context) { companion object { private val TAG = "NotificationModule" private var mCachedBundle: Bundle? = null // 靜態存儲通知內容 private fun sendEvent() { } } override fun getName(): String = "notification" class Receiver : BroadcastReceiver() { override fun onReceive(context: Context?, intent: Intent?) { mCachedBundle = intent?.extras Log.d(TAG, "onReceive: $mCachedBundle") when (intent?.action) { JPushInterface.ACTION_MESSAGE_RECEIVED -> { try { val message = intent.getStringExtra(JPushInterface.EXTRA_MESSAGE) Log.d(TAG, "收到自定義消息: " + message) mEvent = RECEIVE_CUSTOM_MESSAGE if (mRAC != null) { sendEvent() } } (e: Exception) { e.printStackTrace() } } } } } } } ~~~ #### object 表示(object expressions) ##### 使用object表達實現匿名類的實例化 在Java里,經常在需要立刻實現一個接口并override其方法的時候,就會使用的匿名類,例如: ~~~ builder.setNegativeButton(buttons.btnCancel.title, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { // ... } }); ~~~ 改寫成Kotlin就是如下的樣子: ~~~ builder.setNegativeButton(buttons.btnCancel.title, object : DialogInterface.OnClickListener() { override fun onClick(dialogInterface: DialogInterface, I: Int) { // ...} } ~~~ ##### just object 有時候僅僅就是需要一個object(不去繼承某個類或者實現某個接口) ~~~ fun foo() { val adHoc = object { var x: Int = 0 var y: Int = 0 } print(adHoc.x + adHoc.y) } ~~~ 這就類似與JS里對object的靈活定義了。 #### 其他使用的注意事項 ##### 匿名類作為方法返回值 匿名類一般只作為本地或者私有聲明,如果是公開的且沒有繼承的,它可能就是作為Any返回,看例子: ~~~ class C { // Private function, so the return type is the anonymous object type private fun foo() = object { val x: String = "x" } // Public function, so the return type is Any fun publicFoo() = object { val x: String = "x" } fun bar() { val x1 = foo().x // Works val x2 = publicFoo().x // ERROR: Unresolved reference 'x' } } ~~~ ##### 訪問外部變量不需要像Java一樣嚴格為final ~~~ fun countClicks(window: JComponent) { var clickCount = 0 var enterCount = 0 window.addMouseListener(object : MouseAdapter() { override fun mouseClicked(e: MouseEvent) { clickCount++ } override fun mouseEntered(e: MouseEvent) { enterCount++ } }) // ... } ~~~ #### object declaration (object 聲明)與object expressions (object 表達式)的區別 object expressions是立即生效的,而object declaration是懶加載的,只有在使用的時候才生效 companion object的實例化是依賴于它所在的類的加載的
                  <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>

                              哎呀哎呀视频在线观看