2017-07-18 16 views
5

क्या मैं जावा में एक कोटलिन प्रतिनिधि वर्ग का विस्तार कर सकता हूं? नीचे</p> <blockquote> <p>Cannot inherit from final 'Derived'</p> </blockquote> <p>कोड देखें: मैं विस्तार करने के लिए वर्ग सौंपने जावा, एक Kotlin में, और निम्न त्रुटि मिलती है कोशिश कर रहा हूँ

जो मैं करने की कोशिश कर रहा हूं वह कक्षा की एक विधि को सजाने वाला है।

कोई विचार क्यों कोटलिन ने Derived को अंतिम रूप में परिभाषित किया? क्या Derived के लिए अंतिम नहीं होने का कोई तरीका है, इसलिए मैं इसका वारिस कर सकता हूं?

जावा:

new Derived(new BaseImpl(10)) { // Getting the error on this line: `Cannot inherit from final 'Derived'` 
}; 

Kotlin: यहाँ से

interface Base { 
    fun print() 
} 

class BaseImpl(val x: Int) : Base { 
    override fun print() { print(x) } 
} 

class Derived(b: Base) : Base by b 

* उदाहरण: https://kotlinlang.org/docs/reference/delegation.html

उत्तर

9

Kotlin classes are final in by default। यह (Kotlin से या जावा से है) का विस्तार करने के open रूप वर्गांक:

open class Derived(b: Base) : Base by b 

तथ्य यह एक delegating वर्ग जावा इंटरॉप पर कोई प्रभाव नहीं करना चाहिए (हालांकि मैं इसे करने की कोशिश नहीं की है)।

संबंधित मुद्दे

 संबंधित मुद्दे