कहें कि मेरे पास एक इंटरफ़ेस और कुछ कक्षाएं हैं:जावा संग्रह: माता-पिता के संग्रह के रूप में बच्चों का संग्रह पास करें
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(); }
}
और इस मामले में, यह
काम करता है
एक समान प्रश्न http://stackoverflow.com/questions/298305/ है (लेकिन हो सकता है) वहाँ जवाब इंटरफेस तोड़ता है मेरे पास –