2017-05-05 112 views
26

對象和同伴對象在kotlin的一個類中有什麼不同?Kotlin:對象和同伴對象之間的區別

實施例:

class MyClass { 

    object Holder { 
     //something 
    } 

    companion object { 
     //something 
    } 
} 

我已經讀到伴侶對象應被使用,如果含參數/方法密切相關的它的類。

但是爲什麼在類中聲明一個普通對象也有可能呢?因爲它的行爲完全像伴侶,但它必須有一個名字。

它的「靜態」(我來自Java方面)生命週期可能有區別嗎?

+0

用於單身人士的'object'和用於靜態方法的'companion object'。 [Kotlin - 對象聲明](https://kotlinlang.org/docs/reference/object-declarations.html#object-declarations)提供了一個很好的用法說明。 – ArtiomLK

回答

15

對象可以實現接口。在一個類中,定義一個不實現任何接口的簡單對象在大多數情況下都沒有好處。但是,定義實現各種接口的多個對象(例如Comparator)可能非常有用。

就生命週期而言,伴隨對象與在類中聲明的命名對象之間沒有區別。

+0

完美!非常感謝您的解釋! – Poweranimal

+0

AFAIK在初始化順序 – Ilya

+0

有一些區別是什麼區別?我猜伴侶是首先被初始化的,因爲它跟它的類綁定在一起,然後這個對象被調用了? – Poweranimal

2

伴隨對象的存在,因爲您可以調用伴侶對象的功能/屬性,如它是一個Java靜態方法/字段。並且爲什麼你的Holder被允許,那麼沒有理由聲明一個嵌套對象是非法的。它有時可能派上用場。

11
There are two different types of `object` uses, **expression** and **declaration**. 

**Object Expression** 

An object expression can be used when a class needs slight modification, but it's not necessary to create an entirely new subclass for it. Anonymous inner classes are a good example of this. 

    button.setOnClickListener(object: View.OnClickListener() { 
     override fun onClick(view: View) { 
      // click event 
     } 
    }) 

One thing to watch out for is that anonymous inner classes can access variables from the enclosing scope, and these variables do not have to be `final`. This means that a variable used inside an anonymous inner class that is not considered `final` can change value unexpectedly before it is accessed. 

**Object Declaration** 

An object declaration is similar to a variable declaration and therefore cannot be used on the right side of an assignment statement. Object declarations are very useful for implementing the Singleton pattern. 

    object MySingletonObject { 
     fun getInstance(): MySingletonObject { 
      // return single instance of object 
     } 
    } 

And the `getInstance` method can then be invoked like this. 

    MySingletonObject.getInstance() 

**Companion Object** 

A companion object is a specific type of object declaration that allows an object to act similar to static objects in other languages (such as Java). Adding `companion` to the object declaration allows for adding the "static" functionality to an object even though the actual static concept does not exist in Kotlin. Here's an example of a class with instance methods and companion methods. 

class MyClass { 
     companion object MyCompanionObject { 
      fun actsAsStatic() { 
       // do stuff 
      } 
     } 

     fun instanceMethod() { 
      // do stuff 
     } 
    } 

Invoking the instance method would look like this. 

    var myClass = MyClass() 
    myClass.instanceMethod() 

Invoking the companion object method would look like this. 

    MyClass.actsAsStatic() 


See the [Kotlin docs](https://kotlinlang.org/docs/reference/object-declarations.html) for more info. 
+1

謝謝邁克!這應該是答案。 –

+1

如果伴隨對象沒有名稱,則必須將伴隨對象中的方法用作MyClass.MyCompanionObject.actsAsStatic()或MyClass.Companion.actsAsStatic()。這是一個新的變化,還是我做錯了什麼?謝謝。 – Sup

+0

這應該是被接受的答案 – onmyway133

2

對象或對象聲明在初次訪問時被懶惰地初始化。

伴隨對象在加載相應的類時被初始化。它帶來了「靜態」本質,儘管Kotlin本質上不支持靜態成員。

相關問題