SlideShare a Scribd company logo
Использование wildcard
         <?>
Задача



 Требуется написать метод, в который
можно передавать массивы различных
   классов, реализующих заданный
              интерфейс
Решение
void printArray(Number[] array) {...}

printArray(new Integer[] {...});
printArray(new Long[] {...});           Классы наследуются.
printArray(new Float[] {...});          Integer[] является
                                        наследником Number[]




   Вместо Number[] передаём Integer[] – OK!
Задача



Требуется написать метод, в который
можно передавать коллекции различных
классов, реализующих заданный
интерфейс
Проблема
void printList(List<Number> list) {...}

printList(new ArrayList<Integer>());
                                             Типы не наследуются.
printList(new ArrayList<Long>());
                                             List<Integer> не является
printList(new ArrayList<Float>());           наследником
                                             List<Number>




        Вместо List<Number> передаём
        List<Integer> – Compilation error!
Решение
Использовать <?>:

void printList(List<? extends Number> list) {...}

printList(new ArrayList<Integer>());
printList(new ArrayList<Long>());
printList(new ArrayList<Float>());




        Передаём List<Integer> – OK!
Использование <?>
  ● Не стоит использовать <?> в возвращаемых
    значениях: это лишь увеличит количество кода
  ● К тому же:
List<Number> list1 = ...;
list1.add(new Long(1L));                     OK!

List<? extends Number> list2 = ...;
list2.add(new Long(1L));
                                      Compilation error?!

Если вы знаете, почему такое запрещено, скажите нам
пожалуйста. А то мы все головы сломали…

More Related Content

Viewers also liked

Kerberos
KerberosKerberos
Kerberos
AJINKYA PATIL
 
In-depth calendar
In-depth calendarIn-depth calendar
In-depth calendar
In-depth Consulting
 
Prasentation italy germany
Prasentation italy germanyPrasentation italy germany
Prasentation italy germany
Pravin Kumar
 
Keeping the Buzz!
Keeping the Buzz!Keeping the Buzz!
Keeping the Buzz!
Wesley Barnett
 
Resilience 2013
Resilience 2013Resilience 2013
Resilience 2013
In-depth Consulting
 
Verbs & tenses – a brief review v2
Verbs & tenses – a brief review   v2Verbs & tenses – a brief review   v2
Verbs & tenses – a brief review v2
Luiz Pacheco
 
Sap quest
Sap questSap quest
Sap quest
pavankv_85
 
Plan de sesion 25 mayo 2011 solucionado1
Plan de sesion 25 mayo 2011 solucionado1Plan de sesion 25 mayo 2011 solucionado1
Plan de sesion 25 mayo 2011 solucionado1
liztacha
 
Taller digital2
Taller digital2Taller digital2
Taller digital2
liztacha
 
Factors that affect language learning strategies
Factors that affect language learning strategiesFactors that affect language learning strategies
Factors that affect language learning strategies
Fionna Jeffery
 
Wishing you all a happy and lucky 2015
Wishing you all a happy and lucky 2015Wishing you all a happy and lucky 2015
Wishing you all a happy and lucky 2015
In-depth Consulting
 
Extend Eclipse p2 framework capabilities: Add your custom installation steps
Extend Eclipse p2 framework capabilities: Add your custom installation stepsExtend Eclipse p2 framework capabilities: Add your custom installation steps
Extend Eclipse p2 framework capabilities: Add your custom installation steps
Dragos_Mihailescu
 
Manpower consultant
Manpower  consultantManpower  consultant
Manpower consultant
santaclaustm
 
ISO OSI Model (Infosec perspective)
ISO OSI Model (Infosec perspective)ISO OSI Model (Infosec perspective)
ISO OSI Model (Infosec perspective)
AJINKYA PATIL
 
Intercultural Differences in UX
Intercultural Differences in UXIntercultural Differences in UX
Intercultural Differences in UX
eye square
 
Digital Payment Services - Intercultural Differences and Trust as Key Determi...
Digital Payment Services - Intercultural Differences and Trust as Key Determi...Digital Payment Services - Intercultural Differences and Trust as Key Determi...
Digital Payment Services - Intercultural Differences and Trust as Key Determi...
eye square
 
iPhone vs. Samsung Galaxy S II - User Experience Case Study (English)
iPhone vs. Samsung Galaxy S II - User Experience Case Study (English)iPhone vs. Samsung Galaxy S II - User Experience Case Study (English)
iPhone vs. Samsung Galaxy S II - User Experience Case Study (English)
eye square
 

Viewers also liked (17)

Kerberos
KerberosKerberos
Kerberos
 
In-depth calendar
In-depth calendarIn-depth calendar
In-depth calendar
 
Prasentation italy germany
Prasentation italy germanyPrasentation italy germany
Prasentation italy germany
 
Keeping the Buzz!
Keeping the Buzz!Keeping the Buzz!
Keeping the Buzz!
 
Resilience 2013
Resilience 2013Resilience 2013
Resilience 2013
 
Verbs & tenses – a brief review v2
Verbs & tenses – a brief review   v2Verbs & tenses – a brief review   v2
Verbs & tenses – a brief review v2
 
Sap quest
Sap questSap quest
Sap quest
 
Plan de sesion 25 mayo 2011 solucionado1
Plan de sesion 25 mayo 2011 solucionado1Plan de sesion 25 mayo 2011 solucionado1
Plan de sesion 25 mayo 2011 solucionado1
 
Taller digital2
Taller digital2Taller digital2
Taller digital2
 
Factors that affect language learning strategies
Factors that affect language learning strategiesFactors that affect language learning strategies
Factors that affect language learning strategies
 
Wishing you all a happy and lucky 2015
Wishing you all a happy and lucky 2015Wishing you all a happy and lucky 2015
Wishing you all a happy and lucky 2015
 
Extend Eclipse p2 framework capabilities: Add your custom installation steps
Extend Eclipse p2 framework capabilities: Add your custom installation stepsExtend Eclipse p2 framework capabilities: Add your custom installation steps
Extend Eclipse p2 framework capabilities: Add your custom installation steps
 
Manpower consultant
Manpower  consultantManpower  consultant
Manpower consultant
 
ISO OSI Model (Infosec perspective)
ISO OSI Model (Infosec perspective)ISO OSI Model (Infosec perspective)
ISO OSI Model (Infosec perspective)
 
Intercultural Differences in UX
Intercultural Differences in UXIntercultural Differences in UX
Intercultural Differences in UX
 
Digital Payment Services - Intercultural Differences and Trust as Key Determi...
Digital Payment Services - Intercultural Differences and Trust as Key Determi...Digital Payment Services - Intercultural Differences and Trust as Key Determi...
Digital Payment Services - Intercultural Differences and Trust as Key Determi...
 
iPhone vs. Samsung Galaxy S II - User Experience Case Study (English)
iPhone vs. Samsung Galaxy S II - User Experience Case Study (English)iPhone vs. Samsung Galaxy S II - User Experience Case Study (English)
iPhone vs. Samsung Galaxy S II - User Experience Case Study (English)
 

Wildcards in Generic

  • 2. Задача Требуется написать метод, в который можно передавать массивы различных классов, реализующих заданный интерфейс
  • 3. Решение void printArray(Number[] array) {...} printArray(new Integer[] {...}); printArray(new Long[] {...}); Классы наследуются. printArray(new Float[] {...}); Integer[] является наследником Number[] Вместо Number[] передаём Integer[] – OK!
  • 4. Задача Требуется написать метод, в который можно передавать коллекции различных классов, реализующих заданный интерфейс
  • 5. Проблема void printList(List<Number> list) {...} printList(new ArrayList<Integer>()); Типы не наследуются. printList(new ArrayList<Long>()); List<Integer> не является printList(new ArrayList<Float>()); наследником List<Number> Вместо List<Number> передаём List<Integer> – Compilation error!
  • 6. Решение Использовать <?>: void printList(List<? extends Number> list) {...} printList(new ArrayList<Integer>()); printList(new ArrayList<Long>()); printList(new ArrayList<Float>()); Передаём List<Integer> – OK!
  • 7. Использование <?> ● Не стоит использовать <?> в возвращаемых значениях: это лишь увеличит количество кода ● К тому же: List<Number> list1 = ...; list1.add(new Long(1L)); OK! List<? extends Number> list2 = ...; list2.add(new Long(1L)); Compilation error?! Если вы знаете, почему такое запрещено, скажите нам пожалуйста. А то мы все головы сломали…