Skip to content

Instantly share code, notes, and snippets.

@cattaka
Last active May 15, 2016 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cattaka/4d64c7b65815204ac21a984d6fe459fc to your computer and use it in GitHub Desktop.
Save cattaka/4d64c7b65815204ac21a984d6fe459fc to your computer and use it in GitHub Desktop.
public class LearnGenericsAssignment {
static class Func<T> {
T item;
public T getItem() {
return item;
}
public void setItem(T item) {
this.item = item;
}
}
public void func() {
Func<Object> objFunc = new Func<>();
Func<Number> numFunc = new Func<>();
Func<Double> douFunc = new Func<>();
Func<? extends Object> exObjFunc = new Func<>();
Func<? extends Number> exNumFunc = new Func<>();
Func<? extends Double> exDouFunc = new Func<>();
Func<? super Object> suObjFunc = new Func<>();
Func<? super Number> suNumFunc = new Func<>();
Func<? super Double> suDouFunc = new Func<>();
// numFunc = objFunc; // error
numFunc = numFunc;
// numFunc = douFunc; // error
// numFunc = exObjFunc; // error
// numFunc = exNumFunc; // error
// numFunc = exDouFunc; // error
// numFunc = suObjFunc; // error
// numFunc = suNumFunc; // error
// numFunc = suDouFunc; // error
// exNumFunc = objFunc; // error
exNumFunc = numFunc;
exNumFunc = douFunc;
// exNumFunc = exObjFunc; // error
exNumFunc = exNumFunc;
exNumFunc = exDouFunc;
// exNumFunc = suObjFunc; // error
// exNumFunc = suNumFunc; // error
// exNumFunc = suDouFunc; // error
suNumFunc = objFunc;
suNumFunc = numFunc;
// suNumFunc = douFunc; // error
// suNumFunc = exObjFunc; // error
// suNumFunc = exNumFunc; // error
// suNumFunc = exDouFunc; // error
suNumFunc = suObjFunc;
suNumFunc = suNumFunc;
// suNumFunc = suDouFunc; // error
}
public void nestedFunc() {
Func<Func<Object>> objFunc = new Func<>();
Func<Func<Number>> numFunc = new Func<>();
Func<Func<Double>> douFunc = new Func<>();
Func<Func<? extends Object>> exObjFunc = new Func<>();
Func<Func<? extends Number>> exNumFunc = new Func<>();
Func<Func<? extends Double>> exDouFunc = new Func<>();
Func<Func<? super Object>> suObjFunc = new Func<>();
Func<Func<? super Number>> suNumFunc = new Func<>();
Func<Func<? super Double>> suDouFunc = new Func<>();
// numFunc = objFunc; // error
numFunc = numFunc;
// numFunc = douFunc; // error
// numFunc = exObjFunc; // error
// numFunc = exNumFunc; // error
// numFunc = exDouFunc; // error
// numFunc = suObjFunc; // error
// numFunc = suNumFunc; // error
// numFunc = suDouFunc; // error
// exNumFunc = objFunc; // error
// exNumFunc = numFunc; // error
// exNumFunc = douFunc; // error
// exNumFunc = exObjFunc; // error
exNumFunc = exNumFunc;
// exNumFunc = exDouFunc; // error
// exNumFunc = suObjFunc; // error
// exNumFunc = suNumFunc; // error
// exNumFunc = suDouFunc; // error
// suNumFunc = objFunc; // error
// suNumFunc = numFunc; // error
// suNumFunc = douFunc; // error
// suNumFunc = exObjFunc; // error
// suNumFunc = exNumFunc; // error
// suNumFunc = exDouFunc; // error
// suNumFunc = suObjFunc; // error
suNumFunc = suNumFunc;
// suNumFunc = suDouFunc; // error
}
public static class Func1<T> {}
public static class Func2<S> extends Func1<Object> {}
public static class Wrapper<F extends Func1<T>, T> {}
public void hoge() {
Func1<Object> func1 = new Func1<>();
Func2<?> func2 = new Func2<>();
func1 = func2;
Wrapper<Func1<Object>, Object> w1 = new Wrapper<Func1<Object>, Object>();
Wrapper<Func1<Object>, Object> w2 = new Wrapper<Func2<?>, Object>(); // error
Wrapper<? extends Func1<Object>, Object> ew1 = new Wrapper<Func1<Object>, Object>();
Wrapper<? extends Func1<Object>, Object> ew2 = new Wrapper<Func2<?>, Object>();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment