LoginSignup
4
4

More than 5 years have passed since last update.

Javaのgenericsの代入の可否

Last updated at Posted at 2016-05-14

Javaのgenericsを使っているとき、どれからどれへの代入が可能かこんがらがるときがあるので、それ用のメモです。

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
    }
}
4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4