2017-05-05 123 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本质上不支持静态成员。

相关问题