2010-08-03 11 views
7

कहें कि मेरे पास एक इंटरफ़ेस और कुछ कक्षाएं हैं:जावा संग्रह: माता-पिता के संग्रह के रूप में बच्चों का संग्रह पास करें

public interface IPanel<ComponentType extends Component> { 
    public void addComponents(Set<ComponentType> components); 
    public ComponentType create(); 
} 

public class Button extends Component { } 

public class LocalizedButton extends Button { } 

public class ButtonsPanel implements IPanel<Button> { 
    public void addComponents(Set<Button> components) { ... /* uses create() */ ; } 
    public Button create() { return new Button(); } 
} 

public class LocalizedButtonsPanel extends ButtonsPanel { 
    public Button create() { return new LocalizedButton(); } 
} 

तब मेरे पास लोकलाइज्डबटन का एक सेट है और जब मैं

final LocalizedButtonsPanel localizedButtonsPanel = new LocalizedButtonsPanel(); 
final Set<LocalizedButton> localizedButtonsSet = new LinkedHashSet<LocalizedButton>(); 
localizedButtonsPanel.addComponents(localizedButtonsSet); 

पर कॉल करता हूं तो मुझे लगता है कि यह विधि इस तर्क के लिए लागू नहीं है। अगर मैं LocalizedButtonsPanel में addComponents(Set<LocalizedButton> buttons) के रूप में इस विधि को अधिभारित करने का प्रयास करता हूं, तो मुझे निश्चित रूप से टाइप एरर मिलता है।

कुछ पैटर्न याद किया जा सकता है या इस आर्किटेक्चर से निपटने के लिए ट्रिक मौजूद है ताकि स्थानीयकृत बटनों के सेट को सही ढंग से कार्यान्वित किया जा सके?


मुझे जवाब मिल गया है और मैं अपना उदाहरण अधिक ठोस बनाना चाहता हूं - मेरे पास मेरे कार्यान्वयन में कुछ वैधताएं हैं, इसलिए मुझे जेनेरिक के रूप में संग्रहीत करने के लिए संग्रह प्रकार की आवश्यकता है, जो कि सरल कोड है I जवाब का उपयोग कर रहे हैं:

public interface IPanel<ComponentType extends Component, CollectionType extends Collection<? extends Component>> extends Validated<CollectionType> { 
    public void addComponents(CollectionType components); 
    public ComponentType create(); 
} 

public class Button extends Component { } 

public class LocalizedButton extends Button { } 

public class ButtonsPanel implements IPanel<Button, Set<? extends Button>> { 
    public void addComponents(Set<? extends Button> components) { ... /* uses create() */ ; } 
    public Button create() { return new Button(); } 
} 

public class LocalizedButtonsPanel extends ButtonsPanel { 
    public Button create() { return new LocalizedButton(); } 
} 

और इस मामले में, यह

काम करता है
+0

एक समान प्रश्न http://stackoverflow.com/questions/298305/ है (लेकिन हो सकता है) वहाँ जवाब इंटरफेस तोड़ता है मेरे पास –

उत्तर

6

बदलें addComponents()

public void addComponents(Set<? extends Button> components) 

को हस्ताक्षर तो तरीकों बटन के उपवर्गों के सेट स्वीकार करता है कि। इस तरह, आप Set<LocalizedButton> को तर्क के रूप में पास कर सकते हैं, क्योंकि LocalizedButtonButton बढ़ाता है और इसलिए Set<? extends Button> पैरामीटर से मेल खाता है।

+0

ओह हाँ, धन्यवाद।मेरी संरचना थोड़ा और जटिल थी (मैं इस प्रश्न में वर्णन करूंगा), लेकिन मैंने अपनी जेनरिक को आपकी सलाह में फिट करने के लिए सही किया है और यह वास्तव में फिट बैठता है :)। –

5

आपके पास

public class ButtonsPanel implements IPanel<Button> { 
    public void addComponents(Set<Button> components) { ... /* uses create() */ ; } 
    public Button create() { return new Button(); } 
} 

होना चाहिए

public class ButtonsPanel implements IPanel<Button> { 
    public void addComponents(Set<? extends Button> components) { ... /* uses create() */ ; } 
    public Button create() { return new Button(); } 
} 

सूची ही वस्तु है कि उस प्रकार का विस्तार के लिए नहीं बटन के प्रकार के लिए बनाया जाता है।

+1

है: 'पब्लिक क्लास बटन पैनल आईपीनेल लागू करता है ' , काम नहीं करेगा क्योंकि वाइल्डकार्ड' implements' खंड में समर्थित नहीं हैं, और अगर मैं निर्दिष्ट इस: 'सार्वजनिक वर्ग GenericButtonsPanel IPanel ' लागू करता है तो 'LocalizedButtonsPanel फैली GenericButtonsPanel < लोकलाइज्ड बटन> 'इस मामले में' बटनपैनेल 'का बच्चा नहीं होगा जो' जेनेरिकबट्टन पैनेल

+1

सच है, मैंने अपनी पोस्ट संपादित की है। –

0

पैरामीटर प्रकार जावा में कॉन्वर्स नहीं हैं। यह अच्छा है, अन्यथा आप एक सूची < बिल्ली > पर एक कुत्ता जोड़ने में सक्षम होंगे जो सूची < पशु > सूची में उभरा था। आप उपयोग साइट पर covariance जोड़ सकते हैं, उदाहरण के लिए, सूची <? पशु > को एक सूची < बिल्ली > असाइन किया जा सकता है और आप इसकी अतिरिक्त विधि को कॉल नहीं कर सकते हैं।

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