2017-05-05 8 views
26

में ऑब्जेक्ट और साथी ऑब्जेक्ट के बीच अंतर कोटलिन में किसी ऑब्जेक्ट और किसी ऑब्जेक्ट ऑब्जेक्ट के बीच क्या अंतर है?कोटलिन: कक्षा

उदाहरण:

class MyClass { 

    object Holder { 
     //something 
    } 

    companion object { 
     //something 
    } 
} 

मैं पहले से ही है कि साथी वस्तु पढ़ा है, का इस्तेमाल किया जाएगा, यदि युक्त पैरामीटर/तरीकों को बारीकी से अपने वर्ग से संबंधित हैं।

लेकिन कक्षा में सामान्य वस्तु घोषित करने की संभावना क्यों है? क्योंकि यह वास्तव में साथी की तरह व्यवहार करता है, लेकिन इसका नाम होना चाहिए।

क्या इसके "स्थैतिक" (मैं जावा पक्ष से हूं) जीवन चक्र में शायद कोई अंतर हो सकता है?

+0

स्टैटिक तरीकों के लिए सिंगलेट्स और 'साथी ऑब्जेक्ट' के लिए 'ऑब्जेक्ट'। [कोटलिन - ऑब्जेक्ट घोषणाएं] (https://kotlinlang.org/docs/reference/object-declarations.html#object-declarations) एक अच्छा उपयोग स्पष्टीकरण प्रदान करता है। – ArtiomLK

उत्तर

15

ऑब्जेक्ट इंटरफेस को कार्यान्वित कर सकते हैं। एक वर्ग के अंदर, एक साधारण वस्तु को परिभाषित करना जो किसी भी इंटरफेस को लागू नहीं करता है, ज्यादातर मामलों में इसका कोई लाभ नहीं होता है। हालांकि, विभिन्न इंटरफेस को लागू करने वाली कई ऑब्जेक्ट्स को परिभाषित करना (उदा। Comparator) बहुत उपयोगी हो सकता है।

जीवन चक्र के संदर्भ में, एक साथी वस्तु और कक्षा में घोषित नामित वस्तु के बीच कोई अंतर नहीं है। जब पहली बार पहुंच

+0

बिल्कुल सही! आपकी व्याख्या के लिए बहुत बहुत धन्यवाद! – Poweranimal

+0

AFAIK प्रारंभिक क्रम में कुछ अंतर है – Ilya

+0

क्या अंतर है? मुझे लगता है कि साथी पहले शुरू किया गया है, क्योंकि यह अपनी कक्षा से जुड़ा हुआ है और बाद में वस्तु को बुलाया जाता है? – Poweranimal

2

कंपैनियन ऑब्जेक्ट मौजूद है क्योंकि आप साथी वस्तुओं के कार्यों/गुणों को कॉल कर सकते हैं जैसे कि यह एक जावा स्थिर विधि/फ़ील्ड है। और क्यों आपके 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

एक वस्तु या किसी वस्तु घोषणा,, lazily आरंभ नहीं हो जाता।

इसी वर्ग को लोड होने पर एक साथी ऑब्जेक्ट प्रारंभ किया जाता है। यह 'स्थैतिक' सार लाता है, हालांकि कोटलिन मूल रूप से स्थिर सदस्यों का समर्थन नहीं करता है।