2016-02-01 10 views
5

Pomóż mi proszę, aby nie przetestować, dlaczego nie mogę wywołać metody testSuper()? Wystąpił błąd kompilacji:Bez limitu symboli wieloznacznych z rozszerzonymi i super jako parametrami

The method testSuper(Group<? super BClass<?>>) in the type Group <BClass<String>> is not applicable for the arguments (Group<AClass<String>>) 

Ale metoda testExtends() jest OK. Jednak wygląda to tak samo.

class AClass<T> {} 

class BClass<T> extends AClass<T> {} 

class Group<T> { 
    T name; 
    public void testExtends(Group<? extends AClass<?>> value){} 
    public void testSuper(Group<? super BClass<?>> value){} 
    public T getName(){return name;} 
} 

public class GenericTest { 

    public static void GenericTestMethod(){ 

     Group<AClass<String>> instGrA = new Group<AClass<String>>(); 
     Group<BClass<String>> instGrB = new Group<BClass<String>>(); 

     //OK 
     instGrA.testExtends(instGrB); 

     //The method testSuper(Group<? super BClass<?>>) in the type Group <BClass<String>> 
     //is not applicable for the arguments (Group<AClass<String>>) 
     instGrB.testSuper(instGrA); 

    } 
} 

Odpowiedz

3

Istnieje różnica między połączeniami.

W zaproszeniu, które kompiluje,

instGrA.testExtends(instGrB); 

mijamy Group<BClass<String>> do metody, która spodziewa się Group<? extends AClass<?>>. To pasuje, ponieważ BClass<String> jest podtypem AClass<?>> - BClass jest podklasą AClass, a String jest podtypem ?.

Jednak w rozmowie, że nie kompilacji

instGrB.testSuper(instGrA); 

mijamy Group<AClass<String>> do metody, która spodziewa się Group<? super BClass<?>>. To nie pasuje, ponieważ chociaż AClass jest nadklasą BClass, to nie jest nadtypem BClass<?>.

W tym miejscu winić można symbole wieloznaczne z parametrami testExtends i testSuper. Ponieważ w swoich wystąpieniach przypisujesz AClass i BClass do , możesz ich użyć. Mogę to skompilować jeśli zmienimy deklaracje tych metod w Group używać T:

public void testExtends(Group<? extends T> value){} 
public void testSuper(Group<? super T> value){} 
+0

Dzięki za odpowiedź, ale co jeśli wezwanie instGrB.testSuper (instGrB)? BClass to typ BClass . –

+0

Z moimi zmianami, linia ta się kompiluje. – rgettman

+0

tak, kompiluje :) Pytanie jest dlaczego nie kompiluje się z instGrB.testSuper (instGrB), jeśli BClass jest typem BClass ? A może nie? –

Powiązane problemy