10 Mart 2019 Pazar

Generics ve Wilcard Capture Helper

Giriş
Her hangi bir liste içindeki tipe inference ile erişmek için kullanılır. Metodun isim foo ise fooHelper olur. Açıklaması şöyle.
Thanks to the helper method, the compiler uses inference to determine that T is CAP#1, the capture variable, in the invocation.
Örnek
Şöyle yaparız
import java.util.List;

public class WildcardError {

  private void fooHelper(List<T> i){
    i.set(0, i.get(0));
  }
  public void foo(List<?> i){
    fooHelper(i);
  }
}
Örnek
Elimizde şöyle bir kod olsun.
void foo(List<?> i) {
    fooHelper(i);
}
Şöyle yaparız.
<T extends MySuperType> void fooHelper(Class<T> clazz) { }

Hiç yorum yok:

Yorum Gönder